【问题标题】:What is the advantage of adding the [HttpGet] annotation?添加 [HttpGet] 注解有什么好处?
【发布时间】:2014-07-18 18:30:40
【问题描述】:

如果控制器指定了路由:

[Route("api/platypus/getPlatypi")]
public List<Platypus> GetAllPlatypi()
{
    return _platypusRepository.GetPlatypi();
}

...像这样用“[HttpGet]”注释它有什么好处:

[HttpGet]
[Route("api/platypus/getPlatypi")]
public List<Platypus> GetAllPlatypi()
{
   return _platypusRepository.GetPlatypi();
}

?

【问题讨论】:

标签: c# annotations data-annotations asp.net-web-api2 asp.net-web-api-routing


【解决方案1】:

对于您给出的示例,添加 HTTP 方法属性没有任何优势。按照惯例,Web API 将尝试匹配以 HTTP 请求方法(GET、POST、PUT 等)开头的控制器方法。

在您的示例中,方法 GetAllPlatypi 将被视为匹配该控制器的所有 GET 请求。

但是,如果您的方法名为 FindAllPlatypi,则需要添加 [HttpGet] 属性以明确该方法适用于 GET 请求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-27
    • 2020-04-03
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 2010-09-13
    相关资源
    最近更新 更多