Create a complete ASP. NET Web API project, asp. netwebapi Project
Visual Studio provides us with a dedicated tool for creating ASP.. NET Web API application project template. With the help of the Wizard provided by this project template, we can create a complete ASP.. NET Web API project. During project creation, Visual Studio automatically adds Necessary assembly references and configurations for us, and even generates relevant code for us. In a word: this type of project generated by the Wizard is an executable application after it is created.
1. Create a Web API project through VS2013 and. NET 4.5.1
1. Create a project under the solution
2. Select the project web api template. At the bottom of the page, MVC mainly comes with Microsoft API Helper by default and is released using MVC.
3. Security selection
4. Click OK. VS2013 will generate a simple Web API project.
5. Run the project. After the project is started, click the API button on the interface to view the API created by default.
6. Create a Products API
(1) Add ProductController
(2) Select the empty Controller
(3) implement API in ProductController
[RoutePrefix ("api/products")] public class ProductController: ApiController {// get product page API: api/products/product/getList [HttpGet, route ("product/getList")] public Page <Product> GetProductList () {throw new NotImplementedException ();} // obtain a single product API: api/products/product/get? ProductId = product ID [HttpGet, Route ("Product/get")] public product GetProduct (Guid productId) {throw new NotImplementedException ();} // new Product API: api/products/product/add? ProductId = product ID [HttpPost, Route ("Product/add")] public Guid AddProduct (product) {throw new NotImplementedException ();} // new Product API: api/products/product/add? ProductId = product ID [HttpPost, Route ("Product/update")] public void UpdateProduct (Guid productId, product) {throw new NotImplementedException ();} // Product deletion API: api/products/product/delete? ProductId = product ID [HttpDelete, Route ("product/delete")] public void DeleteProduct (Guid productId) {throw new NotImplementedException ();}}
Ii. ASP. NET Web API features
1. Advanced HTTP Programming Model
Use the new strong HTTP object model to directly operate HTTP requests and responses, and use the same programming model and HTTP pipeline on the HTTP client;
2. Support Routing
Web APIs fully support ASP. NET routing, including Routing Parameters and constraints. In addition, the support Convention for action ing eliminates the need to add attributes similar to [HttpPost] to classes or methods;
3. content negotiation
The client and the server can decide the format of the data returned by the API together. XML, JSON, and Form URL-Encoded formats are supported by default. Custom formats can be extended and the default content negotiation policy can be replaced;
4. Model binding and Verification
The model binder can easily extract data from an HTTP request and convert it to a. Net object used in the Action method;
5. Filter
Web APIs support filtering, including the [Authorize] filter tag of the total region. You can add and insert custom filters for actions to implement authentication and exception handling;
6. query Aggregation
As long as Iqueryable <T> is returned, the Web API supports querying through the OData address Convention;
7. Improved Http detail Testability
Web APIs do not set HTTP details to a static Context object, but use HttpRequestMessage and HttpResponseMessage instances. You can use the generic versions of these objects to add custom types for these Http types;
8. Improved Dependency inversion (IoC) Support
Web APIs use the service locator Mode Implemented by MVC Dependency Resolver to obtain instances in different scenarios;
9. Code-Based Configuration
Web APIs use code to complete the configuration separately, which ensures that the configuration file is clean and tidy;
10. Self-Host)
In addition to being hosted in IIS, Web APIs can also be hosted in the process and still use routes and other features.
This section describes how to create a simple Web API project to help you learn it. Later articles will introduce simple examples of Web APIs, I hope you will continue to pay attention to it.