HttpControllerSelector

HttpControllerSelector

本文介绍了在ASP.Net的WebAPI自定义路由处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够成功注册内的global.asax.cs一个Web API路线ALA一个自定义的路由处理程序(从IRouteHandler派生):

  routes.MapHttpRoute(
            名称:DefaultApi
            routeTemplate:{}客户端{/api/1.0/控制器} / {行动} / {ID}
            默认:新{ID = UrlParameter.Optional}
        ).RouteHandler =新SingleActionAPIRouteHandler();

不过,我似乎无法找到一个方法来做到这一点在内存中的主机试图建立一个用于集成测试API时,当我打电话HttpConfiguration.Routes.MapRoute我不能设置一个处理器上返回IHttpRoute。

我应该(通过自定义HttpControllerSelector例如)不同的这样做呢?我明明喜欢做它在两种情况下是一样的。

谢谢,
马特

编辑:

所以我落得这样做基本上遵循从下面的建议,而是简单地覆盖HttpControllerDispatcher类,如下所示:

 公共类CustomHttpControllerDispatcher:HttpControllerDispatcher
{
    公共CustomHttpControllerDispatcher(HttpConfiguration配置):基地(配置)
    {
    }
    保护覆盖任务< Htt的presponseMessage> SendAsync(HTT prequestMessage请求的CancellationToken的CancellationToken)
    {
        //我在这里的东西        返回base.SendAsync(请求的CancellationToken);
    }
}


解决方案

您是非常正确的。自主机返回IHttpRoute并采取HttpMessageHandler作为参数。似乎没有内在的办法特异性的路由处理。

更新:要成为一个更清楚一点

您应该几乎可以肯定在使用HttpControllerSelector和实现一个自定义...一个例子是一展身手。

下面是一个实验位如果HttpControllerSelector不sufficent你要求什么都...原因

但是,正如你可以看到 MapHttpRoute 确实有 HttpMessageHandler 过载,所以你可以用这个实验。 .. if处理程序为NULL,则默认为IHttpController ,但你可以实现你自己的,并以此来引导到正确的控制器...... MVC框架似乎使用 [HttpControllerDispatcher] ,所以借用一些code,你可以在这里把你自己的控制器/路由选择code太 - 你有机会获得途径和选择,并可以进出自己掉它

这CustomHttpControllerDispatcher code是DEMO只...查找行

And perhaps have a play with that...

Example route:

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional },
            constraints: null,
            handler: new CustomHttpControllerDispatcher(config)
        );

Example CustomHttpControllerDispatcher:

public class CustomHttpControllerDispatcher : HttpMessageHandler
{
        private IHttpControllerSelector _controllerSelector;
        private readonly HttpConfiguration _configuration;

        public CustomHttpControllerDispatcher(HttpConfiguration configuration)
        {
            _configuration = configuration;
        }

        public HttpConfiguration Configuration
        {
            get { return _configuration; }
        }

        private IHttpControllerSelector ControllerSelector
        {
            get
            {
                if (_controllerSelector == null)
                {
                    _controllerSelector = _configuration.Services.GetHttpControllerSelector();
                }
                return _controllerSelector;
            }
        }

        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
                return SendAsyncInternal(request, cancellationToken);
        }

        private Task<HttpResponseMessage> SendAsyncInternal(HttpRequestMessage request, CancellationToken cancellationToken)
        {

            IHttpRouteData routeData = request.GetRouteData();
            Contract.Assert(routeData != null);

            //DO SOMETHING CUSTOM HERE TO PICK YOUR CONTROLLER
            HttpControllerDescriptor httpControllerDescriptor = ControllerSelector.SelectController(request);
            IHttpController httpController = httpControllerDescriptor.CreateController(request);

            // Create context
            HttpControllerContext controllerContext = new HttpControllerContext(_configuration, routeData, request);
            controllerContext.Controller = httpController;
            controllerContext.ControllerDescriptor = httpControllerDescriptor;

            return httpController.ExecuteAsync(controllerContext, cancellationToken);
        }
}

这篇关于在ASP.Net的WebAPI自定义路由处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:20