Your current routing strategy is:
1. It takes an optional Id only
"{id}",
in the url
2. It always defaults to Home controller, Index action.
Anything will match this condition as long as it has one item, you app thinks that discussion in http:\localhost:4341\Discussion is a username because it matches.
To fix :
routes.MapRoute(
"Discussion",
"Discussion/{id}",
new { controller = "Home", action = "Discussion", id = UrlParameter.Optional }
routes.MapRoute(
"Default",
"{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
The order of the Mappings is important, reverse these and it will match "Discussion" as a username before it reaches the mapping intended for it.
But I'd be wary of this strategy on a reasonably large application. Assuming you still want the default
"{controller}/{action}/{id}"
with defaults on
controller
(e.g. Home), and
action
(e.g. Index). The first stab might be:
routes.MapRoute(
"Discussion",
"Discussion/{id}",
new { controller = "Home", action = "Discussion", id = UrlParameter.Optional }
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "index", id = UrlParameter.Optional }
routes.MapRoute(
"User",
"{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
This
seems reasonable: you want to check for urls specifying the discussion first, as they'd match both the "standard" and "user" patterns. We cannot put the "User" route before the "Default" as it matches any URL passed in the format http://localhost:4341
/Controller/ . But, equally the default route will match http://localhost:4341
/Username/ , interpreting
username as a controller, (and attempting to access its Index action by default). You can potentially constrain the parts of the url (see
http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-a-route-constraint-cs[
^]) to do what you want, but you may want to think about the need to pass just the username into the URL instead, depending upon your requirements. Even just changing to "/Users/
username" could make things much easier.
Hope this helps & is clear: it seems difficult to explain!