我可以成功地注册一个自定义路由处理程序(派生自IRouteHandler)在global.asax.cs内的一个Web API路由ala:
routes.MapHttpRoute( name: "DefaultApi",routeTemplate: "{client}/api/1.0/{controller}/{action}/{id}",defaults: new{id = UrlParameter.Optional} ).RouteHandler = new SingleActionAPIRouteHandler();
但是,当我调用HttpConfiguration.Routes.MapRoute我无法在返回的IHttpRoute上设置一个处理程序时,我试图在内存主机中设置API进行集成测试时似乎找不到一种方法.
我应该做的不同(例如通过使用自定义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); } }
解决方法
你是对的自主主机返回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 …查找该行
//DO SOMETHING CUSTOM HERE TO PICK YOUR CONTROLLER
也许有一个玩…
示例路线:
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); } }