ASP.NET MVC Url Route supporting (dot)

百般思念 提交于 2019-11-26 08:56:05

问题


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\" />

回答1:


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

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



回答2:


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>



回答3:


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>



回答4:


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



回答5:


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.



来源:https://stackoverflow.com/questions/9273987/asp-net-mvc-url-route-supporting-dot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!