通过httpVerb属性和“ Route”属性进行路由会导致不同的路由吗?

问题描述 投票:0回答:1

我有一个简单的客户控制器(asp net core 3.0),如下所示:

[ApiController]
[Route("api/v1.0/{controller}")]
public class CustomerController : Controller
{
}

通过{id}[HttpGet]设置路由令牌[Route(...)]有什么区别?例如:

    [HttpGet]
    [Route("{id}")]
    public async Task<IActionResult> GetAsync(string id)
    {
       . . .
    }

    [HttpGet("{id}")]
    public async Task<IActionResult> GetAsync(string id)
    {
      . . .
    }

他们会解决类似的路线:api/v1.0/{controller}/{id}

asp.net-core routing attributes asp.net-web-api-routing
1个回答
0
投票

应该与document中的相同:

属性路由还可以使用Http [Verb]属性,例如HttpPostAttribute。所有这些属性都可以接受路由模板。

如果在操作方法上使用[Route(...)],则该操作将接受所有HTTP方法。因此,建议在rest api中使用更具体的Http*Verb*Attributes

[构建REST API时,很少会希望在操作方法上使用[Route(...)],因为该操作将接受所有HTTP方法。最好使用更具体的Http Verb属性来准确了解您的API支持的内容。 REST API的客户端应该知道哪些路径和HTTP动词映射到特定的逻辑操作。

© www.soinside.com 2019 - 2024. All rights reserved.