ASP. NET web API is a framework that can easily build HTTP-based services. It is widely used by clients. Including browsers and mobile devices. ASP. NET Web APIs is an ideal platform for building restful applications.ProgramNetwork framework.
Next, we will start to create the first web API.
Step 1: Obtain mvc4 support. The following link is the Visual Studio 2010 mvc4 patch.
Http://download.microsoft.com/download/2/F/6/2F63CCD8-9288-4CC8-B58C-81D109F8F5A3/AspNetMVC4Setup.exe
Step 2 after installation is complete, start Visual Studio 2010 to create a new project with the mvc4 Template
Click the mvc4 Project template to obtain the mvc4 project type.
Obtain the entire project of the web API
Web API projects have several features
First: the inherited controller is apicontroller.
Second: no corresponding View
Third, the route configuration is not in global. asax. CS, but is separated in a separate app_start folder.
Unified registration in global to facilitate single management. This is also a feature of mvc4.
Registration in global:
App_start directory
The most important webapiconfig
Public Static Void Register (httpconfiguration config) {config. routes. maphttproute (Name: " Defaapi API " , Routetemplate: " API/{controller}/{ID} " , Defaults: New {Id = routeparameter. optional });}
The access method is API/controller/parameter (ID)
Run now to get the effect
Access the default API/values to obtain the following results (captured using the F12 Network)
View detailed access results
Result Source
// Get API/Values
Public Ienumerable get ()
{
Return New String [] { " Value1 " , " Value2 " };
}
Try to return by ID: ModifyCodeIn order to intuitively see changes
// Get API/values/5
Public String Get ( Int ID)
{
Return " Value " + ID;
}
Access API/values/18
View the result
Source code in this example:
/Files/risk/web API 1/mvcapplication1.rar