Wednesday, October 11, 2023

Creating Basic ASP.NET Web API Application

First step

Open Visual Studio and select create new Project






Click on Next



Click on Create





Select Web Api and click on create

The project will be loaded as per the below screen




We need to make a note that Web API Controllers are different from MVC Controllers. In our example, the ValuesController is WebAPI Controller.




From the above screen shot , we can see the Apicontroller is inherited from the ApiController class which is present in sytem.web.http namespace.

Where as , in the below screen HomeController is inherited from Controller which is present System.Web.Mvc namespace.






Now, let us discuss on the ValuesController, we have get/post/put/delete these are nothing but http verbs as shown below






Next go to global.asax file, within that file, we have method called application start, which gets executed only once when the application starts for the first time.

Here , in the below code we are registering the bundles, filters,route


 

Now, go to webapiconfig.Register File in the App_Start Folder as shown below







Here, in the below code, you can see the default route is registered as shown below




Note: If the request controller name does not exists, then we will get the error message as 

No HTTP resource was found that matches the request URI.










No comments:

Post a Comment

Thank you for visiting my blog

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...