Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net web api 在.Net中编写Web Api方法时,是否需要将方法名称前缀与GET、Post等HTTP代码一起使用?_Asp.net Web Api - Fatal编程技术网

Asp.net web api 在.Net中编写Web Api方法时,是否需要将方法名称前缀与GET、Post等HTTP代码一起使用?

Asp.net web api 在.Net中编写Web Api方法时,是否需要将方法名称前缀与GET、Post等HTTP代码一起使用?,asp.net-web-api,Asp.net Web Api,在.Net中编写Web Api方法时,是否需要将方法名称前缀与HTTP代码(如GET、POST等)一起使用? 例如: public IEnumerable GetAllProducts(); 公共IHttpActionResult GetProduct(int id); 公共IHttpActionResult后期产品(产品产品); 不,这不是必需的,但它是将HTTP谓词映射到操作方法的几种约定之一。例如,您可以这样做: [HttpGet] public IHttpActionResult All

在.Net中编写Web Api方法时,是否需要将方法名称前缀与HTTP代码(如
GET
POST
等)一起使用? 例如:

public IEnumerable GetAllProducts();
公共IHttpActionResult GetProduct(int id);
公共IHttpActionResult后期产品(产品产品);

不,这不是必需的,但它是将HTTP谓词映射到操作方法的几种约定之一。例如,您可以这样做:

[HttpGet]
public IHttpActionResult AllProducts();
或者这个:

public IHttpActionResult GetAllProducts();

它们都可以处理
获取
请求。

如果您使用属性路由,则不需要这样做

[Route("api/books")]
[HttpPost]
public HttpResponseMessage CreateBook(Book book) { ... }

例如:

下面的示例将CreateBook方法映射到HTTP POST请求

[Route("api/books")]
[HttpPost]
public HttpResponseMessage CreateBook(Book book) { ... }

所以它意味着每个方法都应该有属性或Get word作为前缀…正确吗?是的。中有更多详细信息(在该页面上搜索“http方法”,该部分无法链接)。