https://exceptionnotfound.net/using-http-methods-correctly-in-asp-net-web-api/

The algorithm ASP.NET uses to calculate the "default" method for a given action goes like this:

https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/routing-and-action-selection

Action Selection

After selecting the controller, the framework selects the action by calling the IHttpActionSelector.SelectAction method. This method takes an HttpControllerContext and returns an HttpActionDescriptor.

The default implementation is provided by the ApiControllerActionSelector class. To select an action, it looks at the following:

  • The HTTP method of the request.
  • The "{action}" placeholder in the route template, if present.
  • The parameters of the actions on the controller.

Before looking at the selection algorithm, we need to understand some things about controller actions.

Which methods on the controller are considered "actions"? When selecting an action, the framework only looks at public instance methods on the controller. Also, it excludes "special name" methods (constructors, events, operator overloads, and so forth), and methods inherited from the ApiController class.

HTTP Methods. The framework only chooses actions that match the HTTP method of the request, determined as follows:

  1. You can specify the HTTP method with an attribute: AcceptVerbs, HttpDelete, HttpGet, HttpHead, HttpOptions, HttpPatch, HttpPost, or HttpPut.
  2. Otherwise, if the name of the controller method starts with "Get", "Post", "Put", "Delete", "Head", "Options", or "Patch", then by convention the action supports that HTTP method.
  3. If none of the above, the method supports POST.

Parameter Bindings. A parameter binding is how Web API creates a value for a parameter. Here is the default rule for parameter binding:

  • Simple types are taken from the URI.
  • Complex types are taken from the request body.

Simple types include all of the .NET Framework primitive types, plus DateTime, Decimal, Guid, String, and TimeSpan. For each action, at most one parameter can read the request body.

Note:It is possible to override the default binding rules. See WebAPI Parameter binding under the hood.

微软官方的parameter binding文章

https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

With that background, here is the action selection algorithm.

  1. Create a list of all actions on the controller that match the HTTP request method.

  2. If the route dictionary has an "action" entry, remove actions whose name does not match this value.

  3. Try to match action parameters to the URI, as follows:

    1. For each action, get a list of the parameters that are a simple type, where the binding gets the parameter from the URI. Exclude optional parameters.
    2. From this list, try to find a match for each parameter name, either in the route dictionary or in the URI query string. Matches are case insensitive and do not depend on the parameter order.
    3. Select an action where every parameter in the list has a match in the URI.
    4. If more that one action meets these criteria, pick the one with the most parameter matches.
  4. Ignore actions with the [NonAction] attribute.

Step #3 is probably the most confusing. The basic idea is that a parameter can get its value either from the URI, from the request body, or from a custom binding. For parameters that come from the URI, we want to ensure that the URI actually contains a value for that parameter, either in the path (via the route dictionary) or in the query string.

Routing and Action Selection in ASP.NET Web API的更多相关文章

  1. Asp.Net Web API VS Asp.Net MVC

    http://www.dotnet-tricks.com/Tutorial/webapi/Y95G050413-Difference-between-ASP.NET-MVC-and-ASP.NET-W ...

  2. ASP.NET Web API 2.0新特性:Attribute Routing1

    ASP.NET Web API 2.0新特性:Attribute Routing[上篇] 对于一个针对ASP.NET Web API的调用请求来说,请求的URL和对应的HTTP方法的组合最终决定了目标 ...

  3. Asp.net web api 知多少

    本系列主要翻译自<ASP.NET MVC Interview Questions and Answers >- By Shailendra Chauhan,想看英文原版的可访问http:/ ...

  4. ASP.NET Web API中的Routing(路由)

    [译]Routing in ASP.NET Web API 单击此处查看原文 本文阐述了ASP.NET Web API是如何将HTTP requests路由到controllers的. 如果你对ASP ...

  5. Routing in ASP.NET Web API和配置文件的设定读取

    Routing Tables In ASP.NET Web API, a controller is a class that handles HTTP requests. The public me ...

  6. Routing in ASP.NET Web API

    Why is HttpGet required only for some actions? https://stackoverflow.com/questions/28068868/why-is-h ...

  7. 如何让ASP.NET Web API的Action方法在希望的Culture下执行

    在今天编辑推荐的<Hello Web API系列教程--Web API与国际化>一文中,作者通过自定义的HttpMessageHandler的方式根据请求的Accep-Language报头 ...

  8. ASP.NET Web API是如何根据请求选择Action的?[下篇]

    ASP.NET Web API是如何根据请求选择Action的?[下篇] 再<上篇>中我们简单介绍了用于实现Action选择机制的HttpActionSelector,接下来我们来讨论本章 ...

  9. ASP.NET Web API是如何根据请求选择Action的?[上篇]

    ASP.NET Web API是如何根据请求选择Action的?[上篇] Web API的调用请求总是针对定义在某个HttpController中的某个Action方法,请求响应的内容来源于调用目标A ...

随机推荐

  1. SetForegroundWindow以及 如何将一个某个窗口提到最顶层(转)

    http://hi.baidu.com/gookings/item/2b7912ca8d5b3625a0b50aa2 SetForegroundWindow 函数功能:该函数将创建指定窗口的线程设置到 ...

  2. python之MySQL学习——输出指定条件的结果集

    # 引入pymysql模块 import pymysql as pm # 数据库连接 db = pm.connect(host=",database='task', charset='utf ...

  3. 单源最短距离 Single Source Shortest Path

    单源最短距离_示例程序_图模型_用户指南_MaxCompute-阿里云 https://help.aliyun.com/document_detail/27907.html 单源最短距离 更新时间:2 ...

  4. If 条件控制 & while循环语句

    Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执行过程: if 语句 Python中if语句的一般形式如下所示: i ...

  5. Learn How To Cross Over The Wall

    1.一个proxy的实现 http://blog.codingnow.com/2011/05/xtunnel.html 2.SOCK5 RFC http://www.faqs.org/rfcs/rfc ...

  6. 3509.com 纵横天下虚拟主机,垃圾中的战斗机

    被纵横天下主机(3509.com)这间垃圾公司气疯了,他们公司自己要更换server(空间).居然把我挂在上面的站点数据弄丢了.并且更换连一封Email通知都没有.更离谱的是,跟他们反映这个情况后.他 ...

  7. 解决python中write()函数向文件中写中文时出现乱码的问题

    今天看<python编程从入门到实践>的第10章文件.异常,在做练习的时候,向文件中写内容,但是写中文就不行,后来在百度上查了众多资料,解决方法如下: 解决:在open()函数中添加一个e ...

  8. centos7 重启网卡失败

    今天在centOS 7 network服务重启不了 现把各种解决方法归纳整理,希望能让后面的同学少走点歪路... 首先看问题:执行service network restart命令后出现下面的错误: ...

  9. 011-JDK可视化监控工具-Jstat

    一.概述 Jstat 是JDK自带的一个轻量级小工具.全称“Java Virtual Machine statistics monitoring tool”,它位于java的bin目录下,主要利用JV ...

  10. Window mysql 安装配置

    1.下载 安装包 mysql-noinstall--winx64.zip 2.解压缩zip包 安装路径 C:\Program Files\MySQL\mysql--winx64 3.配置环境变量 添加 ...