The way you have your routing setup, the View_Article
route will never be hit unless you are generating the URL since the URL /Discussion/Articlev1/Details/id/cool-html-article
will match the Discussion_default
route.
First of all, put them in the correct order (from most specific to least specific):
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"View_Article",
"Discussion/Articlev1/Details/{id}/{articleTitle}",
new { controller = "Articlev1", action = "Details", id = UrlParameter.Optional, articleTitle = UrlParameter.Optional }
);
context.MapRoute(
"Discussion_default",
"Discussion/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
From there, it is easy to change the first URL to whatever you like, as long as you take care to ensure there are no URL conflicts in your entire configuration.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"View_Article",
"Discussion/Articlev1/{id}/{articleTitle}",
new { controller = "Articlev1", action = "Details", id = UrlParameter.Optional, articleTitle = UrlParameter.Optional }
);
context.MapRoute(
"Discussion_default",
"Discussion/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
NOTE: It also probably doesn't make sense to have a View_Article
URL that doesn't have an id
, so you shouldn't make id = UrlParameter.Optional
in that case.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"View_Article",
"Discussion/Articlev1/{id}/{articleTitle}",
new { controller = "Articlev1", action = "Details", articleTitle = UrlParameter.Optional }
);
context.MapRoute(
"Discussion_default",
"Discussion/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
Of course, this means if you have a controller in your application named ArticleV1Controller
, the View_Article
route will match (and set the id
parameter to the action you have passed in the URL). If you can't live with having the /Details
segment hard coded into the URL, then you will need to differentiate it another way, such as using a route constraint. The following assumes your ID must be all digits:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "View_Article",
url: "Discussion/Articlev1/{id}/{articleTitle}",
defaults: new { controller = "Articlev1", action = "Details", articleTitle = UrlParameter.Optional },
constraints: new { id = @"\d+" }
);
context.MapRoute(
name: "Discussion_default",
url: "Discussion/{controller}/{action}/{id}",
defaults: new { action = "Index", id = UrlParameter.Optional }
);
}