ASP.Net WebAPI中的自定义路由处理程序

时间:2012-09-11 13:50:12

标签: asp.net asp.net-web-api

我能够在global.asax.cs中为Web API路由ala成功注册一个自定义路由处理程序(从IRouteHandler派生):

        routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "{client}/api/1.0/{controller}/{action}/{id}",
            defaults: new{id = UrlParameter.Optional}
        ).RouteHandler = new SingleActionAPIRouteHandler();

然而,当我尝试为集成测试设置内存主机时,我似乎无法找到一种方法,当我调用HttpConfiguration.Routes.MapRoute时,我无法在上面设置处理程序返回IHttpRoute。

我应该以不同方式执行此操作(例如,使用自定义HttpControllerSelector)吗?在两种情况下,我显然都喜欢这样做。

谢谢, 马特

编辑:

所以我最终做的是基本上遵循下面的建议,但只是覆盖HttpControllerDispatcher类,如下所示:

public class CustomHttpControllerDispatcher : HttpControllerDispatcher
{
    public CustomHttpControllerDispatcher(HttpConfiguration configuration) : base(configuration)
    {
    }
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        // My stuff here

        return base.SendAsync(request, cancellationToken);
    }
}

1 个答案:

答案 0 :(得分:13)

你是对的。自托管返回IHttpRoute并将HttpMessageHandler作为参数。似乎没有内置的方法来处理路由处理程序。

更新:更清楚

你几乎肯定应该使用HttpControllerSelector并实现一个自定义的...一个例子。 http://netmvc.blogspot.co.uk/

以下是一些实验如果HttpControllerSelector不足以满足您的要求......

但是,您可以看到MapHttpRoute确实有HttpMessageHandler的重载,因此您可以尝试使用此... if the handler is NULL then it defaults to IHttpController,但您可以实现自己的并使用它来指导到正确的控制器... MVC框架似乎使用[HttpControllerDispatcher] here,所以借用一些代码,您也可以在这里放置自己的控制器/路由选择代码 - 您可以访问路由和选择器可以自己交换进出。

此CustomHttpControllerDispatcher代码仅适用于DEMO ...查找行

  

//在这里定制选择你的控制器

也许玩这个......

示例路线:

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

示例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);
        }
}