4

This is probably a simple question but I just can't get it to work.

I've got this route specified in my RouteConfig

routes.MapRoute(
    name: "DefaultSiteRoute",
    url: "{accountid}/{hostname}/{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, accountid = UrlParameter.Optional, hostname = UrlParameter.Optional  }
);

And it works fine for a url like this

/123456/www.test.com/

or this

/123456/www.test.com/Controller/Action

but it can't cope with this

/123456/www.test.com

I get an IIS 404

What is stranger is if I call Url.Action for that route with the default Controller and Action (ie Home/Index) it creates a url without a trailing slash, which it then doesn't recognise. I really need it to work with and without the trailing slash.

Glenn Slaven
  • 33,720
  • 26
  • 113
  • 165

1 Answers1

6

Problem is ASP.net 4.0 doesn't route URLs that ends with an extension to MVC. They do this in order to speed up requests to static files. See this link

What you can do:

1) Configure UrlRoutingModule to route all managed and un-managed requests (default is only route managed requests).

Drawback: May be bad for performance.

<system.webServer>
    <modules>
     <remove name ="UrlRoutingModule-4.0"/>
      <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="runtimeVersionv4.0" />
    </modules>
<system.webServer>

2) Configure to handle .com, .net. org etc extensions

Drawback: Feels like a hack.

   <system.webServer>
       <handlers>
          <add name="UrlRoutingHandler"
           type="System.Web.Routing.UrlRoutingHandler, 
                 System.Web, Version=4.0.0.0, 
                 Culture=neutral, 
                 PublicKeyToken=b03f5f7f11d50a3a"
                 path="*.com"
                 verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"/>
LostInComputer
  • 15,188
  • 4
  • 41
  • 49
  • Option 1 works great with one small addition, you need to add `runAllManagedModulesForAllRequests="true"` to the modules tag. otherwise, that's it. I completely forgot that it would be interpreting the .com as an extension, good call! – Glenn Slaven Nov 14 '13 at 13:51
  • Option 1 without setting runAllManagedModulesForAllRequests to true works in IIS Express. Didn't test with IIS though. – LostInComputer Nov 14 '13 at 14:00