directory
RESTful Design Points
1. Websites should not have verbs, only nouns, and should use plural.
2. Parameters in a GET request are generally in the URL
RESTful API examples
So what are GET, PUT, POST and DELETE
Start creating the project
Create the Help page
routing
Routing prefix
Hidden API
parameter
multi-parameter
The parameter constraint
Optional parameters
Multiple version management of apis
Is RESTful design a must
Refer to the article
RESTful Design Points
1. Websites should not have verbs, only nouns, and should use plural.
For example: None of the following is RESTful
Xxx.com/api/getuser…
Xxx.com/api/getuser…
Xxx.com/api/updateu…
2. Parameters in a GET request are generally in the URL
Such as:
xxx.com/api/users Obtain the user information list
Xxx.com/api/users/t… Get user information list of male and 18 years old with multiple parameters
Xxx.com/api/users/1… Obtain user information with ID 1001
The following rules are not RESTful
Xxx.com/api/users?i…
Xxx.com/api/users?i…
RESTful API examples
GET xxx.com/api/users // Obtain the information list of all users
The GET xxx.com/api/users/1… // Obtain information about user 1001
PUT xxx.com/api/users/1… // Modify the information about user 1001
POST xxx.com/api/users // Add user information
The DELETE xxx.com/api/users/1… // Delete user 1001
So what are GET, PUT, POST and DELETE
When I first came into contact with these words, I was also ignorant
At the beginning of contact with WebAPI, I belong to back-end development, what front-end JS, request and so on know little, it seems that I still need to know more
GET, PUT, POST, and DELETE are the front-end data request methods, which correspond to the add, DELETE, modify, and query functions of RESTful apis
When looking up information on the Internet, many are written so, for a back-end developer, or do not understand, the next face I explain one by one
When the front end requests data, there are various parameters, including a type parameter, which can be filled in the request mode
Such as:
Get the user information list
$. Ajax ({url: "/ API /users/", // request address type: "GET", // request address data: {}, // parameter success: function (data) {// request success, do what}, error: Function (data) {// what to do if the request fails}});Copy the code
Modifying User Information
$. Ajax ({url: "/ API/users / 1001", / / request address type: "PUT", the way / / request data: {" ID ": 1001," name ":" haha "}, / / parameter success: Function (data) {error: function (data) {// error: function (data) {// error: function (data) {// error: function (data) {// error: function (data) {// failure}});Copy the code
Adding User Information
$. Ajax ({url: "/ API/users / / request address type:" POST ", / / requests data: {" name ":" haha "}, / / parameter success: Function (data) {error: function (data) {// error: function (data) {// error: function (data) {// error: function (data) {// error: function (data) {// failure}});Copy the code
Deleting User Information
$. Ajax ({url: "/ API /users/1001", // request address type: "DELETE", // request method data: {}, // parameter success: Function (data) {error: function (data) {// error: function (data) {// error: function (data) {// error: function (data) {// error: function (data) {// failure}});Copy the code
Start creating the project
Take a look at the example provided by Microsoft
The methods are Get, Post, Put, and Delete respectively, corresponding to add, Delete, change, and search respectively
public class ValuesController : ApiController
{
// GET: api/Values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET: api/Values/5
public string Get(int id)
{
return "value";
}
// POST: api/Values
public void Post([FromBody]string value)
{
}
// PUT: api/Values/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE: api/Values/5
public void Delete(int id)
{
}
}
Copy the code
**[HttpGet], [HttpPut], [HttpPost], [HttpPost], [HttpDelete]
For instance,
Create the Help page
The WebAPI project I created with VS2019 already contains the Help page, the old version does not,
Take a look at the Web API help page here and add comments to the help document to add comments
routing
The default route is API /+{controller name}/+ parameter, such as API /Values/5
We can also add the Route feature to modify the Route
The effect
Routing prefix
We can set the RoutePrefix for the entire controller using the RoutePrefix feature
In the case of route prefixes, you can also rewrite a single route using the ~ symbol
The effect
Hidden API
Add the ApiExplorerSettings feature to make the API not displayed on the Help page, and also add it to the controller to hide all of the controller’s apis.
I tried, and although the API for the Help page is hidden, it can still be called
parameter
multi-parameter
For example, access to information about users of a particular gender and age
Get a list of male and age 18 users at xxx.com/api/users/t…
The parameter constraint
Can be used to restrict parameters
For example, restrict isMan to boo and age to int
[Route("{isMan:bool}/{age:int}")]
Copy the code
The constraint
Optional parameters
Usually written this way, there is a parameter comment added to the Help page. The default value is 18
or
The default age is 18
Visit xxx.com/api/users/t…
Multiple version management of apis
RESTful multi-version management design
Is RESTful design a must
Zhihu on this article, everyone has everyone’s opinion
www.zhihu.com/question/36…
And I think using RESTful design is the norm, the norm at the front and back end
If you’re going to use RESTful style, follow it, not half-and-half
Many users say that when a GET request has too many parameters, it is not RESTful
And I think, when there are a lot of parameters, can’t you split the API, divide an API into two, does not mean that there can only be one method, and the parameters are really so many
Take a look at the official Microsoft example, for example
Get book list information, which can be divided into multiple apis
The use of RESTful design has always been controversial, and this article is also my own opinion, please point out if it is not well written. thank you
Refer to the article
Understanding RESTful Architecture
www.ruanyifeng.com/blog/2011/0…
RESTful Design Guide
www.ruanyifeng.com/blog/2014/0…
Microsoft official Web API document routing Settings
Docs.microsoft.com/zh-cn/aspne…