a blog for those who code

Wednesday, 1 February 2017

Getting Started with Asp.Net Web API

In this post we will be discussing about Getting Started with Asp.Net Web API. Asp.Net Web API is a framework for building Web API's (HTTP based services) on top of the .Net Framework that can be accessed from various clients such as browsers, mobile devices, desktop applications etc.


Is Web API Restful ?


One thing to note that ASP.NET Web API doesn't dictate any specific architectural style that means the API's need not have to be restful. Even though you can build a Restful service on top of it. You might ask why shouldn't we use WCF for restful services? We can create WCF for creating restful services but to do that we need to write a lot of configuration and stuff thus more natural choice for creating restful services would be ASP.Net Web API.

Why should I choose Web API over WCF ?


We should use WCF over ASP.NET Web API when we are creating services that are not dependent on transport protocol (i.e. single services with multiple endpoints) because WCF supports numerous protocols and messaging formats whereas Web API supports only HTTP-based services. Say for example you have two clients, one of them needs TCP whereas other need HTTP as their transport protocol. So to serve both the clients with the single services you will need WCF with some configurations.

Check the detailed differences between WCF and ASP.NET Web API in

PC : https://msdn.microsoft.com/en-us/library/jj823172.aspx


What is HTTP Get, Post, Put and Delete Request Verb


Lets take an example of a database row where we can have four actions which we can perform and they are :

1. Reading a row (i.e. Get Request Verb)
2. Creating a row (i.e. Post Request Verv)
3. Updating a row (i.e. Put Request Verb)
4. Deleting a row (i.e. Delete Request Verb)

Creating a Web API Project


You need to create a new Project and select ASP.NET Web Application. After clicking OK you will be asked to select templates. There choose Web API as shown below :


After creating the project you can see that the folder structure is similar to MVC project with Model, View and Controller folders. If you go inside Controller folder, you can see that we have a Web API Controller i.e. ValuesController class which inherits from ApiController.

Next if you go inside App_Start folder, you can see that you have a WebApiConfig class where you have all the configuration of the Web API. It also contains details about Web API Route (routeTemplate) as shown below :


Next is to run the application. After running if we navigate to /api/values it will be firing a get request in our Values Controller and get the values (value1 and value2) back. If we specify the value id i.e. /api/values/1 it will be sending the value1 back as shown below.


Conclusion


So you are now up and running with Web API and know What is Web API and when to use it. You also know why you should choose Web API over WCF. You have also created a demo project with basic controller having get, post, put and delete request.

Please Like and Share the CodingDefined Blog, if you find it interesting and helpful.

No comments:

Post a Comment

Contact Us

Name

Email *

Message *

Sponsers

CC License