ASP.NET Web API Basics for Beginners

What is Web API?

ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework. (from MSDN)

You can use Web API to feed your web, mobile or desktop applications with data.
Some Aspects of Web API Framework
  • Web API can be used with Web Forms, Web Pages or ASP.NET MVC.
  • It is a great way to make RESTful services of any kind.
  • These RESTful services can return any kind of resource. (Images, text, JSON, XML, binary files etc.)
  • Response format (type) is formed automatically according to the request headers. (This is called content negotiation.)
  • Controller method to invoke is determined by the URI and the request type. (This is called Web API Routing which is similar in some way to ASP.NET MVC Routing)
  • Enables usage of clear and user friendly URLs.
  • Provides easy semantics to route an handle incoming requests.
  • Built on ASP.NET core like ASP.NET MVC.
  • Has a flexible design.
  • Can be hosted on Non-Web applications. (You can use Web API to create a local Http Service and use it on your local machine. This is called Self Hosting.)
  • Like MVC, Web API has Control classes with Action Methods (or Actions). These classes handle Http requests.
Basic Request Routing
When the Web API Framework receives a request it is directed to an Action in a Controller class. Consider the following Route Template and corresponding URI:
Route Template: api/{controller}/{id}
URI: api/products/1
When the request arrives, “products” is concatenated with “controller” to produce “ProductsController”. So, framework knows that the Action to be invoked is in the “ProductsController” controller class.
Which action to invoke is determined by the type of the request. If it is a “Get” request, then the Framework looks for an Action whose name starts with “Get…” and accepts one argument (1).
If you have an Action whose signature is like “GetProductDetails(int id)” in “ProductsController” controller class, “api/products/1” URI with a request method of “Get” will invoke GetProductDetails Action.
If you have an action whose signature is like “PostMyValues(int id)” in “ProductsController” controller class, the same “api/products/1” URI with a request method of “Post” will invoke PostMyValues Action.
Note: This is just the basics of routing. To find more info on routing and actions follow these links:
Routing in ASP.NET Web API:
Routing and Action Selection:
If you are an ASP.NET developer and also familiar with MVC concept, it should be quite easy for you to learn and use Web API.

To learn more about Web API:

Hope this helps someone.

Good luck,

Leave a Reply

Your email address will not be published. Required fields are marked *