Introduction to ASP.NET Web API Introduction 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. With Web API content negotiation, we can return data based on the client requests. What I mean is, if the client is requesting the data to be returned as JSON or XML, the Web API framework deals with the request type and returns the data appropriately based on the media type. By default Web API provides JSON and XML based responses.39923
Web API is an ideal platform for building pure HTTP based services where the request and response happens with HTTP protocol. The client can make a GET, PUT, POST, and DELETE request and get the Web API response appropriately.
In summary, the Web API is
• An HTTP Service
• Designed for broad reach
• Uses HTTP as an Application protocol, not a transport protocol
Web API Architecture
We shall see below the Web API architecture when you are hosting Web API in ASP.NET and self-hosting through console or Windows service.
Routing configuration in Web API is slightly different from asp.NET MVC Routing. The Web API uses Http Route Collection and Route. The Web API team has reused the routing logic of MVC in Web API. The only reason why it’s a different routing is in order to keep the Web API from having its dependency onasp.NET Routing. The team decided to make it independent so that Web API will not haveasp.NET class dependencies and it can be hosted in console or a Windows service as self-hosting.
While inasp.NET Web API Routing, the system will not only register the Http Route object but also it will create a wrapper Route object and in the asp.NET routing engine.
The significant difference that you can see here is you will not have access to Routing data in message handlers when you are trying to access the same in self-hosting, the reason is the route configuration is set and it will run at a later point in the life cycle.
The other significant difference between the API Controller and the normal MVC controller is with Web API , API Controller, the actions are dispatched by default based on the HTTP request. However there is a flexibility to override it so that the Web API will use the action name to select the action method within the API Controller.
What is Content Negotiation in Web API
This is something which you will here frequently in Web API. Content negotiation is the process of selecting the best representation for a given response when there are multiple representations available. The underling Web API framework implements the content negotiation and that is the reason why and how the client can request data with a specific media type.
By default the Web API returns data in JSON format, however while requesting for a resource, we can specify the media type to return so that the Web API knows what you are requesting for and select the proper formatter to output the data.
How to Implement Web API?
We shall see a generic implementation of Web API.
We have to create a class which derives from API Controller. The methods defined in the Web API controller map to the HTTP methods. If you have a method name prefixed with GET, it means you are trying to return some data based upon the GET request. You just have to make sure that whatever actions you are implementing must be prefixed with the right request type (GET, POST, PUT, and DELETE).
Note: The method name need not be Get () or Post () etc. However it does not limit to prefixing the request types. You can still implement the actions with different naming but you have to make sure to use the suitable action filters [Http Get], [Post], [Put], [Delete].
Background
This article is just a start of a beginner article for Web API. A basic understanding of MVC is sufficient to understand and implement asp.NET Web API.
B.原文的翻译 ASP.NET Web API的介绍英文文献和中文翻译:http://www.751com.cn/fanyi/lunwen_40647.html