Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are very right. Self host returns IHttpRoute and takes HttpMessageHandler as a parameter. There seems no inbuilt way to specificity a route handler.</p> <p><strong>Update: To be a bit clearer</strong></p> <p>You should almost certainly have a go at using a HttpControllerSelector and implementing a custom one... An example being. <a href="http://netmvc.blogspot.co.uk/" rel="noreferrer">http://netmvc.blogspot.co.uk/</a></p> <p><strong>What follows is a bit of experimentation</strong> if the HttpControllerSelector is not sufficent for your requirements for what ever reason...</p> <p>But, as you can see the <code>MapHttpRoute</code> does have an overload for <code>HttpMessageHandler</code> so you can experiment with this... <a href="http://msdn.microsoft.com/en-us/library/system.web.http.routing.ihttproute.handler%28v=vs.108%29.aspx" rel="noreferrer">if the handler is NULL then it defaults to IHttpController</a> but you can implement your own and use this to direct to the correct controller... The MVC framework appears to use <code>[HttpControllerDispatcher]</code> <a href="http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/f59892797e46#src/System.Web.Http/Dispatcher/HttpControllerDispatcher.cs" rel="noreferrer">here</a>, so borrowing some code you can place your own controller / route selection code here too - you have access to the route and selector and can swap it in and out yourself.</p> <p>This CustomHttpControllerDispatcher code is for DEMO only... look for the line </p> <blockquote> <p>//DO SOMETHING CUSTOM HERE TO PICK YOUR CONTROLLER</p> </blockquote> <p>And perhaps have a play with that...</p> <p><strong>Example route:</strong></p> <pre><code> config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional }, constraints: null, handler: new CustomHttpControllerDispatcher(config) ); </code></pre> <p><strong>Example CustomHttpControllerDispatcher:</strong></p> <pre><code>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&lt;HttpResponseMessage&gt; SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { return SendAsyncInternal(request, cancellationToken); } private Task&lt;HttpResponseMessage&gt; 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); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload