Search code examples
c#asp.net-mvcpathrequestcontext

Parse MVC route custom file extension from RequestContext


I have set up two custom extensions to enable MVC in IIS6.

So the site can be accessed with a URL of either something like...

mysite/mycontroller.europe/myaction

or like...

mysite/mycontroller.america/myaction

What is the most robust way of finding the extension from the RequestContext instance?

So I would like to be able to write something like...

var location = reqContext.......GetExtenstion(); // location = "europe"

and obviously have that work even if the setup of the site/directories changes a little.


Solution

  • Define a route:

    routes.MapRoute(
        "DefaultWithExtension",
        "{controller}.{extension}/{action}",
        new { controller = "Home", action = "Index", extension = "america" }
    );
    

    and then:

    var extension = RequestContext.RouteData.GetRequiredString("extension");