目录

  1. ApiController

  2. HttpActionDescriptor

  3. IHttpActionSelector

ApiController

在上节中,讲到如何选择并激活对应的IHttpController,而一般我们在开发中使用的是ApiController

public abstract class ApiController : IHttpController, IDisposable
{
public virtual Task<HttpResponseMessage> ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)
{
this.Initialize(controllerContext);
HttpActionDescriptor actionDescriptor = services.GetActionSelector().SelectAction(controllerContext);
HttpActionContext actionContext = new HttpActionContext(controllerContext,actionDescriptor);
return services.GetActionInvoker().InvokeActionAsync(actionContext, cancellationToken);
}
}

在ApiController中,我们看到通过内置的DI容器选择出对应的HttpActionDescriptor.本节重点内容就是介绍SelectAction方法.

HttpActionDescriptor

在介绍IHttpActionSelector前,我们需要了解HttpActionDescriptor

public abstract class HttpActionDescriptor
{
//相关联的HttpControllerDescriptor
public HttpControllerDescriptor ControllerDescriptor { get; } { set; }
//Action名称(通过使用ActionName特性,Action名称可以和方法名称不同)
public abstract string ActionName { get; }
//Action返回值类型(void为null)
public abstract Type ReturnType { get; }
//支持的HttpMethod(默认一个Action方法支持一种HttpMethod,且默认为Post Method,可以使用AcceptVerbs特性支持多个)
public virtual Collection<HttpMethod> SupportedHttpMethods { get; }
//Action方法所有参数
public abstract Collection<HttpParameterDescriptor> Parameters { get; };
}

而HttpActionDescriptor默认实现为ReflectedHttpActionDescriptor,这里稍微展示下初始化HttpActionDescriptor过程

public class ReflectedHttpActionDescriptor : HttpActionDescriptor
{
//初始化HttpActionDescriptor的属性
public ReflectedHttpActionDescriptor(HttpControllerDescriptor controllerDescriptor, MethodInfo methodInfo)
: base(controllerDescriptor)
{
this.InitializeProperties(methodInfo);
this._parameters = new Lazy<Collection<HttpParameterDescriptor>>(() => this.InitializeParameterDescriptors());
} private Collection<HttpParameterDescriptor> InitializeParameterDescriptors()
{
return this.ParameterInfos.Select(item => new ReflectedHttpParameterDescriptor((HttpActionDescriptor) this, item)).ToList();
}
}

IHttpActionSelector

IHttpActionSelector是选择Action最核心的类

public interface IHttpActionSelector
{
//选择符合标准的Action
HttpActionDescriptor SelectAction(HttpControllerContext controllerContext); //获取HttpController所有Action
ILookup<string, HttpActionDescriptor> GetActionMapping(HttpControllerDescriptor controllerDescriptor);
}

IHttpActionSelector默认的实现为ApiControllerActionSelector

public class ApiControllerActionSelector : IHttpActionSelector
{
ILookup<string, HttpActionDescriptor> GetActionMapping(HttpControllerDescriptor controllerDescriptor);
{
//找到所有符合要求的方法
var methods = controllerDescriptor.ControllerType
.GetMethods(BindingFlags.Instance | BindingFlags.Public), methodInfo =>
!methodInfo.IsSpecialName && !methodInfo.GetBaseDefinition().DeclaringType.IsAssignableFrom(TypeHelper.ApiControllerType)
&& methodInfo.GetCustomAttribute<NonActionAttribute>() == null);
return methods.Select(method => new HttpActionDescriptor(controllerDescriptor,method))...;
}
}

注意:GetActionMapping会在请求每个HttpController第一次的时候 缓存当前所有HttpActionDescriptor

当我们找到HttpController下所有HttpActionDescriptor,还需要最后一步通过SelectAction筛选出最终的Action

由于这块源码稍微复杂,这里把关键的几步及对应的方法名说明下

public virtual HttpActionDescriptor SelectAction(HttpControllerContext controllerContext)
{
1. Action名称过滤 如果路由中配置Action变量,则会先过滤ActionName.调用方法GetInitialCandidateList 2. Http方法过滤 对于Get Put Post默认都通过,调用方法FindActionsForVerb 3. 必须的参数能绑定上 在Action方法上的必须参数,在路由参数和Request参数中能获取到,调用方法FindActionMatchRequiredRouteAndQueryParameters 4. 参数个数符合最多的匹配 当多个Action方法都满足以上3个条件时,取最多参数符合的那个,调用方法FindActionMatchMostRouteAndQueryParameters 5. 取唯一匹配
最终匹配结果为1个Action方法则成功,多个或零失败,在SelectAction方法本身调用
}

这里稍微举个例子来解释SelectAction

public class DemoController : ApiController
{
public string Get(int x) public string Get(int x, int y) public string Get(string x, string y) public string Get(string x, string y, string z)
}

分别请求如下地址,并列出对应的匹配方法

  • demo?x=1 OK

    public string Get(int x)

  • demo?x=1&y=2 Erro

    public string Get(int x, int y)

    public string Get(string x, string y)

  • demo?x=1&y=2&z=3 OK

    public string Get(string x, string y, string z)

(OK表示请求成功,Erro表示请求失败)

备注:

- Action的选择明显比Controller的激活要复杂

- 文章中的代码并非完整WebAPI代码,一般是经过自己精简后的.

- 本篇内容使用MarkDown语法编辑

首发地址:http://neverc.cnblogs.com/p/5956432.html

[Web API] Web API 2 深入系列(4) Action的选择的更多相关文章

  1. ASP.NET Web API - ASP.NET MVC 4 系列

           Web API 项目是 Windows 通信接口(Windows Communication Foundation,WCF)团队及其用户激情下的产物,他们想与 HTTP 深度整合.WCF ...

  2. 如何用Baas快速在腾讯云上开发小程序-系列1:搭建API & WEB WebSocket 服务器

    版权声明:本文由贺嘉 原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/221059001487422606 来源:腾云阁 h ...

  3. Java web与web gis学习笔记(二)——百度地图API调用

    系列链接: Java web与web gis学习笔记(一)--Tomcat环境搭建 Java web与web gis学习笔记(二)--百度地图API调用 JavaWeb和WebGIS学习笔记(三)-- ...

  4. HTML5权威指南--Web Storage,本地数据库,本地缓存API,Web Sockets API,Geolocation API(简要学习笔记二)

    1.Web Storage HTML5除了Canvas元素之外,还有一个非常重要的功能那就是客户端本地保存数据的Web Storage功能. 以前都是用cookies保存用户名等简单信息.   但是c ...

  5. 初试ASP.NET Web API/MVC API(附Demo)

    写在前面 HTTP RESTful 创建Web API 调用Web API 运行截图及Demo下载 ASP.NET Web API是​​一个框架,可以很容易构建达成了广泛的HTTP服务客户端,包括浏览 ...

  6. 我所理解的RESTful Web API [Web标准篇]

    REST不是一个标准,而是一种软件应用架构风格.基于SOAP的Web服务采用RPC架构,如果说RPC是一种面向操作的架构风格,而REST则是一种面向资源的架构风格.REST是目前业界更为推崇的构建新一 ...

  7. 重构Web Api程序(Api Controller和Entity)续篇

    昨天有写总结<重构Web Api程序(Api Controller和Entity)>http://www.cnblogs.com/insus/p/4350111.html,把一些数据交换的 ...

  8. web api写api接口时返回

    web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢,下面就介绍两种方法: 方法一:(改配置法) 找到Global.asax文件,在Applic ...

  9. Google Maps API Web Services

    原文:Google Maps API Web Services 摘自:https://developers.google.com/maps/documentation/webservices/ Goo ...

随机推荐

  1. Weblogic反序列化漏洞补丁更新解决方案

    Weblogic反序列化漏洞的解决方案基于网上给的方案有两种: 第一种方案如下 使用SerialKiller替换进行序列化操作的ObjectInputStream类; 在不影响业务的情况下,临时删除掉 ...

  2. wpf 逻辑树与可视化树

    XAML天生就是用来呈现用户界面的,这是由于它具有层次化的特性.在WPF中,用户界面由一个对象树构建而成,这棵树叫作逻辑树.逻辑树的概念很直观,但是为什么要关注它呢?因为几乎WPF的每一方面(属性.事 ...

  3. ASP.NET Core Kestrel 中使用 HTTPS (SSL)

    在ASP.NET Core中,如果在Kestrel中想使用HTTPS对站点进行加密传输,可以按照如下方式 申请证书 这一步就不详细说了,有免费的和收费的,申请完成之后会给你一个*.pfx结尾的文件. ...

  4. 10年微软MVP路(如何成为一个MVP?)

    搞微软技术的,大家或多或少都有听说过微软的"最有价值专家"(MVP), 从2006年到2015年连续10年ASP.NET/IIS MVP.当年很多一起搞微软技术的朋友都转搞其他非微 ...

  5. 解读ASP.NET 5 & MVC6系列(3):项目发布与部署

    本章我们将讲解ASP.NET5项目发布部署相关的内容,示例项目以我们前一章创建的BookStore项目为例. 发布前的设置 由于新版ASP.NET5支持多版本DNX运行环境的发布和部署,所以在部署之前 ...

  6. [实践] Android5.1.1源码 - 让某个APP以解释执行模式运行

    [实践] Android5.1.1源码 - 让某个APP以解释执行模式运行   作者:寻禹@阿里聚安全 前言 本文的实践修改了Android5.1.1的源码. 本文只简单的讲了一下原理.在“实践”一节 ...

  7. ABP理论学习之通知系统

    返回总目录 本篇目录 介绍 订阅通知 发布通知 用户通知管理者 实时通知 通知存储 通知定义 介绍 通知(Notification)用于告知用户系统中的特定事件.ABP提供了基于实时通知基础设施的发布 ...

  8. 浅谈WEB跨域的实现(前端向)

    同源策略/SOP(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,浏览器很容易受到XSS.CSFR等攻击(可以参考我的这篇文章). SOP要求 ...

  9. Kruskal 最小生成树算法

    对于一个给定的连通的无向图 G = (V, E),希望找到一个无回路的子集 T,T 是 E 的子集,它连接了所有的顶点,且其权值之和为最小. 因为 T 无回路且连接所有的顶点,所以它必然是一棵树,称为 ...

  10. EasyPR--一个开源的中文车牌识别系统

    我正在做一个开源的中文车牌识别系统,Git地址为:https://github.com/liuruoze/EasyPR. 我给它取的名字为EasyPR,也就是Easy to do Plate Reco ...