As a side note, I understand the whole ambiguous controller names problem and have used namespacing to get my routes working, so I don't think that is an issue here.
So far I have my project level controllers and then a User Area with the following registration:
public class UserAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "User";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"UserHome",
"User/{id}",
new { action = "Index", controller = "Home", id = 0 },
new { controller = @"Home", id = @"d+" }
);
context.MapRoute(
"UserDefault",
"User/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
The "UserHome" route is there so I can allow the route /User/5
vs. /User/Home/Index/5
which looks cleaner IMO.
Ideally I would like to use Url.RouteUrl("UserHome", new { id = 5 })
, to generate the route elsewhere, but this always either comes back blank or gives me an exception saying it cannot find the route name, which is obviously there.
However when I use Url.RouteUrl("UserHome", new { controller = "Home", action = "Index", id = 5 })
it works no problem.
Why do I have to specify the action and controller when they have defaults already in the route mapping? What am I missing?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…