Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Url Route supporting (dot)

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" /> 
like image 969
Cindro Avatar asked Feb 14 '12 08:02

Cindro


People also ask

How you can define a route in ASP.NET MVC?

Routing in ASP.NET MVC cs file in App_Start Folder, You can define Routes in that file, By default route is: Home controller - Index Method. routes. MapRoute has attributes like name, url and defaults like controller name, action and id (optional).

What are the 3 segments of the default route?

The Default route maps the first segment of a URL to a controller name, the second segment of a URL to a controller action, and the third segment to a parameter named id.

How routing is done in the MVC explain with suitable example?

In MVC, routing is a process of mapping the browser request to the controller action and return response back. Each MVC application has default routing for the default HomeController. We can set custom routing for newly created controller. The RouteConfig.


1 Answers

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> 
like image 82
Klaas Coenraads Avatar answered Sep 19 '22 17:09

Klaas Coenraads