有什么方法可以更改.NET MVC WebApi操作的默认ActionVerb吗?

时间:2017-11-22 05:47:53

标签: c# asp.net-mvc asp.net-web-api

根据Microsoft Documentation,在动作选择一节中,它指出:

  
      
  1. 如果以上都不是,则该方法支持POST。
  2.   

有没有办法更改此默认行为以将HttpGet用作操作的默认ActionVerb?

1 个答案:

答案 0 :(得分:1)

您链接的文档引用了IHttpActionSelector.SelectAction方法作为操作选择的来源,而ApiControllerActionSelector是默认实现。

如果您查看the source code,就可以看到the default implementation of that interface确实是ApiControllerActionSelector类。如果查看ApiControllerActionSelector实现,您可以看到它认为是有效路由的每种方法的it creates a ReflectedHttpActionDescriptor。然后,当您查看ReflectedHttpActionMethodyou can see how the default of POST is applied时,所有其他记录的识别替代动词的策略都会失败。

因此,要回答您的问题,您可以编写一个新的IHttpActionSelector实现,该实现返回HttpActionDescriptor,它使用的默认行为与您在ApiControllerActionSelector中找到的默认行为不同。

或者,您可以应用[HttpGet]属性和/或使用“获取”前缀命名该操作并将其称为一天。