Visual Studio provides us with a project template dedicated to creating ASP.net Web API applications, and with the help of the wizards provided by this project template, we can create a complete ASP.net Web API project "one-click". During project creation, Visual Studio automatically adds the necessary assembly references and configurations to us, and even automatically generates the relevant code for us, in a Word: This wizard-generated project is itself an executable application after being created.
First, create a Web API project through VS2013,. NET 4.5.1
1, the new project under the solution
2, select the Project Web API template, at the bottom of the MVC is mainly default will bring Microsoft API Helper, using MVC release
3. Safe choice
4, click OK button, VS2013 will generate a simple Web API project
5, run the project, start the project, click on the Interface API button to see the default created by the API
6. Create a new products API
(1), add Productcontroller
(2), select Empty Controller
(3) Implementation of API in Productcontroller
[Routeprefix ("Api/products")] public class Productcontroller:apicontroller {//Get product pagination Api:api/products/product /getlist [HttpGet, Route ("Product/getlist")] public page<product> getproductlist () {throw new No
Timplementedexception (); //Get a single product api:api/products/product/get?productid= Product ID [HttpGet, Route ("Product/get")] public product Getpro
Duct (Guid productId) {throw new NotImplementedException (); ///Products new api:api/products/product/add?productid= Product ID [HttpPost, Route ("Product/add")] public Guid addproduct (
Product product) {throw new NotImplementedException (); ///Products new api:api/products/product/add?productid= Product ID [HttpPost, Route ("Product/update")] public void Updatepr
Oduct (Guid productId, product product) {throw new NotImplementedException (); ///Product deletion api:api/products/product/delete?productid= Product ID [httpdelete, Route ("Product/delete")] public void De LetEproduct (Guid productId) {throw new NotImplementedException ();
}
}
second, asp.net Web API features
1, the Advanced HTTP programming model
Use the new strongly typed HTTP object model to manipulate HTTP requests and responses directly, using the same programming model and HTTP pipelines on HTTP clients;
2. Support Routing
The Web API fully supports ASP.net routing, including routing parameters and Constraints. In addition, to the Mapping Support Convention for actions, you no longer need to add attributes like [HttpPost] to a class or method;
3. Content Consultation
The client and the server can determine the format of the API return data together. XML, JSON, and form url-encoded are supported by default, and can be extended to add custom formats and even replace the default content negotiation strategy;
4. Model Binding and validation
The model binder can easily extract data from an HTTP request and convert it into a. Net object that is used in an action method.
5, Filtration
The Web API supports filtering, including the generally known [authorize] filter tag, which can add and insert custom filters for the Action, authentication, exception handling, etc.
6. Query Aggregation
As long as the simple return iqueryable<t>, the Web API will support the query through the OData address Convention;
7. Improved Http detail testability
Instead of setting HTTP details on a static context object, the Web API uses the Httprequestmessage and Httpresponsemessage instances to add custom types to these HTTP types using the generic version of these objects;
8. Improved dependency inversion (IoC) Support
The Web API uses the service locator pattern implemented by MVC Dependency Resolver to capture instances in different scenarios;
9, code-based configuration
The Web API uses code-complete configuration alone to ensure that the configuration files are clean and tidy;
10. Self-hosting (self-host)
The Web API can be hosted in the process, in addition to being hosted in IIS, and can still be used for routing and other features.
The above is the full content of this article, how to create a simple Web API project, I hope to help everyone's learning, and then the article will introduce a simple example of the Web API, I hope you continue to pay attention.