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" />
Cindro
  • 1,055
  • 4
  • 12
  • 23
  • http://stackoverflow.com/questions/8163401/dot-symbol-in-url – Chuck Norris Feb 14 '12 at 08:53
  • 1
    I have the same issue, but in IIS 6. – Chuck Norris Feb 14 '12 at 08: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. – Darin Dimitrov Feb 14 '12 at 09:58
  • 1
    No Darin, you misunderstood me, http://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 '12 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 '12 at 13:42
  • try posting up your entire routing table, my immediate guess is another route is conflicting and causing issues. – Anthony Shaw Feb 14 '12 at 14:40
  • I have actually commented out ALL other routes in Global.asax, the above one is the ONLY one left. If I type localhost/john.lee it does not work. If I type localhost/john.lee/ it works and the page comes up. How can I make it so it adds a slash in the background though the slash isn't present on the address bar and not add a slash when its there on the address bar? maybe this would be a shortcut? – Cindro Feb 15 '12 at 00:06
  • just to be sure, did you mean that `` does or does not work? Making a sample mvc3 application with one route very similar to your own works as long as `runAllManagedModulesForAllRequests` is true. IIRC, the issue you seem to be having would be caused by IIS trying to map your url to a file of type `lee` named `john` rather than your route. – parKing Oct 24 '12 at 15:16
  • possible duplicate of [Dots in URL causes 404 with ASP.NET mvc and IIS](http://stackoverflow.com/questions/11728846/dots-in-url-causes-404-with-asp-net-mvc-and-iis) – Tolga Evcimen Sep 09 '14 at 06:27

5 Answers5

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>
dove
  • 20,469
  • 14
  • 82
  • 108
Klaas Coenraads
  • 639
  • 5
  • 3
  • I needed this for my api controller and it worked like a charm! – batzen Jan 31 '13 at 22:52
  • Very elegant solution. Thanks! – Ivan Stoyanov Jan 02 '14 at 09:41
  • Thank you! I was able to use this to solve http://stackoverflow.com/questions/11728846/dots-in-url-causes-404-with-asp-net-mvc-and-iis and http://stackoverflow.com/questions/9331516/asp-net-mvc-routing-add-html-extension-to-routes – user1454265 Feb 12 '14 at 18:43
  • 4
    I really don't get how this works; `Cannot create an abstract class.` http://pastebin.com/vuQ2WnWZ – Korijn Oct 08 '14 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 02 '15 at 06:31
  • What does `System.Web.Routing.UrlRoutingHandler` do? Cannot find an explanation why this works – Pylyp Lebediev Oct 05 '20 at 13:52
  • Very old ansser that solves my current problem. Thanks!!! – Mike Apr 22 '22 at 15:10
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>
Community
  • 1
  • 1
V.B.
  • 6,236
  • 1
  • 33
  • 56
  • 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 '15 at 13:25
  • So, could anyone explain what is the actual difference between TransferRequestHandler and UrlRoutingHandler? Which of them should be preferred? – Serg Aug 08 '17 at 13:25
  • Does anyone still uses IIS7 stack in 2017!? .NET Core and Kestrel are so much better! – V.B. Aug 08 '17 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>
Satish Singh
  • 2,169
  • 3
  • 23
  • 32
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
Mark
  • 11
  • 1
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.

Marnix van Valen
  • 13,265
  • 4
  • 47
  • 74