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.

5
  • Do you need trailing slash in all links? Commented Nov 14, 2013 at 8:12
  • I don't need it, but given that the Url.Action method generates a link without the slash it's going to be a problem Commented Nov 14, 2013 at 8:24
  • What happens if you make {action} parameter optional? Commented Nov 14, 2013 at 9:04
  • @CodeART giving it a default value (Index) should already make it "optional". Commented Nov 14, 2013 at 9:16
  • @HenkMollema that is correct, all parameters in that route are optional, but 2 have default values specified Commented Nov 14, 2013 at 10:36

1 Answer 1

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"/>
Sign up to request clarification or add additional context in comments.

2 Comments

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!
Option 1 without setting runAllManagedModulesForAllRequests to true works in IIS Express. Didn't test with IIS though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.