39

I hope that you can help me with the below problem.

I am using ASP.NET MVC 3 on IIS7 and would like my application to support username's with dots.

Example: http://localhost/john.lee

This is how my Global.asax looks like: (http://localhost/{username})

routes.MapRoute(
    "UserList",
    "{username}",
    new { controller = "Home", action = "ListAll" }
);

The applications works when I access other pages such as http://localhost/john.lee/details etc.

But the main user page doesn't work, I would like the app to work like Facebook where http://www.facebook.com/john.lee is supported.

I used below code and it didn't work for me at all:

<httpRuntime relaxedUrlToFileSystemMapping="true" />

I was able to use below code and get the app to accept dots but I definitely wouldn't like to use below code for many different reason, please tell me there is a way to overcome this problem.

<modules runAllManagedModulesForAllRequests="false" />
9
  • 1
    I have the same issue, but in IIS 6. Feb 14, 2012 at 8:54
  • 1
    I am unable to reproduce the issue. It works for me in IIS Express (so it should also work in IIS 7.0+). Also you seem to be indicating an url of the form /john.lee/details but according to your route definition you cannot specify an action. It will always use the ListAll action. So the problem is not really related to the dot here. It's probably more about your routes. Feb 14, 2012 at 9:58
  • 1
    No Darin, you misunderstood me, localhost/john.lee is using the above route (ListAll), but /john.lee/details is using a different route of course, but somehow that works. I think this has something to do with IIS, by enabling a dot on usernames, it would allow something like localhost/default.aspx (default.aspx being a username and not a file on the root folder). I really need to find a way around this.
    – Cindro
    Feb 14, 2012 at 13:40
  • 1
    Basically by enabling dots on the username, someone's username could be default.aspx while another person's username could be john.lee but IIS will get confused by that i pressume?
    – Cindro
    Feb 14, 2012 at 13:42

5 Answers 5

63

Add a UrlRoutingHandler to the web.config. This requires your url to be a bit more specific however (f.e. /Users/john.lee). This forces every url starting with /Users to be treated as a MVC url:

<system.webServer>    
  <handlers>      
    <add name="UrlRoutingHandler" 
         type="System.Web.Routing.UrlRoutingHandler, 
               System.Web, Version=4.0.0.0, 
               Culture=neutral, 
               PublicKeyToken=b03f5f7f11d50a3a" 
         path="/Users/*" 
         verb="GET"/>      
  </handlers>
</system.webServer>
6
  • I needed this for my api controller and it worked like a charm!
    – batzen
    Jan 31, 2013 at 22:52
  • Thank you! I was able to use this to solve stackoverflow.com/questions/11728846/… and stackoverflow.com/questions/9331516/… Feb 12, 2014 at 18:43
  • 4
    I really don't get how this works; Cannot create an abstract class. pastebin.com/vuQ2WnWZ
    – Korijn
    Oct 8, 2014 at 11:35
  • At first I didn't think this worked, then I realized I was putting it in ~/Views/web.config instead of ~/web.config. Ashame you have to put the full absolute path especially when the app's deployment virtual directory might change.
    – danludwig
    Jan 2, 2015 at 6:31
  • What does System.Web.Routing.UrlRoutingHandler do? Cannot find an explanation why this works Oct 5, 2020 at 13:52
4

Just add this section to Web.config, and all requests to the route/{*pathInfo} will be handled by the specified handler, even when there are dots in pathInfo. (taken from ServiceStack MVC Host Web.config example and this answer https://stackoverflow.com/a/12151501/801189)

This should work for both IIS 6 & 7. You could assign specific handlers to different paths after the 'route' by modifying path="*" in 'add' elements

  <location path="route">
    <system.web>
      <httpHandlers>
        <add path="*" type="System.Web.Handlers.TransferRequestHandler" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" />
      </httpHandlers>
    </system.web>
    <!-- Required for IIS 7.0 -->
    <system.webServer>
      <modules runAllManagedModulesForAllRequests="true" />
      <validation validateIntegratedModeConfiguration="false" />
      <handlers>
        <add name="ApiURIs-ISAPI-Integrated-4.0" path="*" type="System.Web.Handlers.TransferRequestHandler" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" preCondition="integratedMode,runtimeVersionv4.0" />
      </handlers>
    </system.webServer>
  </location>
3
  • 2
    This seems to work as well as using System.Web.Routing.UrlRoutingHandler - I'm just not sure which is better or more efficient - UrlRoutingHandler seems to be specifically geared towards routing.
    – Ripside
    Apr 28, 2015 at 13:25
  • So, could anyone explain what is the actual difference between TransferRequestHandler and UrlRoutingHandler? Which of them should be preferred?
    – Serg
    Aug 8, 2017 at 13:25
  • Does anyone still uses IIS7 stack in 2017!? .NET Core and Kestrel are so much better!
    – V.B.
    Aug 8, 2017 at 17:11
2

I was facing the same issue. So the best solution for me is:

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true"></modules>
<system.webServer>
1

For anyone getting an 'Cannot create abstract class' exception when using the UrlRoutingHandler approach, it's likely due to:

  • Using a restricted 'path' (e.g. path="/Files/*") in your web.config declaration, and
  • A folder/path with the same name exists in your project
0

I don't think the dot is the problem here. AFAIK the only char that should not be in the user name is a /

Without seeing the route that matches john.lee/details it's hard to say what's wrong, but I'm guessing that you have another route that matches the url, preventing the user details route from being matched correctly.

I recommend using a tool like Glimpse to figure out what route is being matched.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.