通过 via httpVerb 属性和 "Route" 属性进行路由会导致不同的路由吗?
Will routing via via httpVerb attribute and "Route" attribute result in different routes?
我有一个简单的客户控制器 (asp net core 3.0),如下所示:
[ApiController]
[Route("api/v1.0/{controller}")]
public class CustomerController : Controller
{
}
通过 [HttpGet]
或 [Route(...)]
设置路由令牌 {id}
有何不同
例如:
[HttpGet]
[Route("{id}")]
public async Task<IActionResult> GetAsync(string id)
{
. . .
}
[HttpGet("{id}")]
public async Task<IActionResult> GetAsync(string id)
{
. . .
}
他们会解析到类似的路线吗:api/v1.0/{controller}/{id}
?
应该是一样的,来自document :
Attribute routing can also make use of the Http[Verb] attributes such as HttpPostAttribute. All of these attributes can accept a route template.
如果在操作方法上使用 [Route(...)]
,该操作将接受所有 HTTP 方法。所以建议在休息时使用更具体的 Http*Verb*Attributes
api :
When building a REST API, it's rare that you will want to use [Route(...)] on an action method as the action will accept all HTTP methods. It's better to use the more specific HttpVerbAttributes to be precise about what your API supports. Clients of REST APIs are expected to know what paths and HTTP verbs map to specific logical operations.
我有一个简单的客户控制器 (asp net core 3.0),如下所示:
[ApiController]
[Route("api/v1.0/{controller}")]
public class CustomerController : Controller
{
}
通过 [HttpGet]
或 [Route(...)]
设置路由令牌 {id}
有何不同
例如:
[HttpGet]
[Route("{id}")]
public async Task<IActionResult> GetAsync(string id)
{
. . .
}
[HttpGet("{id}")]
public async Task<IActionResult> GetAsync(string id)
{
. . .
}
他们会解析到类似的路线吗:api/v1.0/{controller}/{id}
?
应该是一样的,来自document :
Attribute routing can also make use of the Http[Verb] attributes such as HttpPostAttribute. All of these attributes can accept a route template.
如果在操作方法上使用 [Route(...)]
,该操作将接受所有 HTTP 方法。所以建议在休息时使用更具体的 Http*Verb*Attributes
api :
When building a REST API, it's rare that you will want to use [Route(...)] on an action method as the action will accept all HTTP methods. It's better to use the more specific HttpVerbAttributes to be precise about what your API supports. Clients of REST APIs are expected to know what paths and HTTP verbs map to specific logical operations.