ASP.NET WebApi simple example

Create a new project: (vs version 2019)

 

 Create an empty project, select the reference on the right: webapi

 

Create a new controller 

 

The code is as follows:

 

Call the first method:

Return result:

 

Call the second method:

 

Return result:

 

Call the third method:

 Return result:

 

Call the fourth method:

 Return result:

 

Regarding the post request, there is a place to pay attention to. In the api interface method, the model object (class) must be used to receive the parameter. If you use the variable (string Id, string Name), this will cause problems. Many pits.

 

Another point is that the created WebApi uses the Restful style by default. I personally feel that this style is very inconvenient to use, so I changed the routing mode in the WebApiConfig.cs file to match the controller name and method name.

Guess you like

Origin blog.csdn.net/liangmengbk/article/details/109148332