A solution that works for your case but is not recommended
You have a predefined set of controllers (usually less than 10) in your application, so you can set a restriction on the controller name and then redirect everything else to the user profile:
routes.MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new { controller = "Home|Admin|Reports|..." } ); routes.MapRoute( "Profile", "{username}/{action}", new { controller = "Profile", action = "Details" } );
But this will not work if some username matches the name of your controller. This is a small opportunity, based on experience, ends with empirical data, but this is not a 0% chance. When the username matches some controller, it automatically means that it will be processed by the first route, because the restrictions will not fail him.
Recommended Solution
A better way would be to have URL requests like:
www.mydomain.com/profile/username
Why do I recommend this? This will make Becasue much easier and cleaner and allow you to have several different profile pages:
- more
www.mydomain.com/profile/username - settings
www.mydomain.com/profile/username/settings - posts
www.mydomain.com/profile/username/messages - and etc.
The route definition in this case will be as follows:
routes.MapRoute( "Profile", "Profile/{username}/{action}", new { controller = "Profile", action = "Details" } ); routes.MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional } );