Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8563783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:02:38+00:00 2026-06-11T17:02:38+00:00

I’m able to successfully register a custom route handler (deriving from IRouteHandler) inside global.asax.cs

  • 0

I’m able to successfully register a custom route handler (deriving from IRouteHandler) inside global.asax.cs for a Web API route ala:

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

However I can’t seem to find a way to do this when trying to setup an in memory host for the API for integration testing, when I call HttpConfiguration.Routes.MapRoute I’m not able to set a handler on the returned IHttpRoute.

Should I be doing this differently (for instance by using a custom HttpControllerSelector)? I’d obviously like to do it the same way in both cases.

Thanks,
Matt

EDIT:

So what I ended up doing was basically following the advice from below, but simply overriding the HttpControllerDispatcher class as follows:

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:02:40+00:00Added an answer on June 11, 2026 at 5:02 pm

    You are very right. Self host returns IHttpRoute and takes HttpMessageHandler as a parameter. There seems no inbuilt way to specificity a route handler.

    Update: To be a bit clearer

    You should almost certainly have a go at using a HttpControllerSelector and implementing a custom one… An example being. http://netmvc.blogspot.co.uk/

    What follows is a bit of experimentation if the HttpControllerSelector is not sufficent for your requirements for what ever reason…

    But, as you can see the MapHttpRoute does have an overload for HttpMessageHandler so you can experiment with this… if the handler is NULL then it defaults to IHttpController but you can implement your own and use this to direct to the correct controller… The MVC framework appears to use [HttpControllerDispatcher] here, 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.

    This CustomHttpControllerDispatcher code is for DEMO only… look for the line

    //DO SOMETHING CUSTOM HERE TO PICK YOUR CONTROLLER

    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);
            }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.