I have an area by the name Admin
, and in AdminAreaRegistration
i define route like blow:
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new {Controller="Home", action = "Index", id = UrlParameter.Optional }
);
context.MapRoute(
name: "Product",
url: "Admin/ProductForm-{FormName}",
defaults: new { controller = "ProductForm", action = "Index", id = UrlParameter.Optional }
);
}
}
and in ProductFormController
(in Admin
area) i have :
public ActionResult Index(string FormName)
{
return View();
}
when i want to go to this url:http://localhost:5858/Admin/ProductForm-mobile
, it should route to Index
action in the ProductFormController
(with FormName=mobile), but it didn't. what is the problem?
In ASP.NET MVC the route mechanism works in a very simple way, at the first match will redirect to that pattern, ignoring the upcoming ones. That's why the Default Route should always be the last.
You need to move your "Admin_defaut"
route to the end.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new {Controller="Home", action = "Index", id = UrlParameter.Optional }
);
context.MapRoute(
name: "Product",
url: "Admin/ProductForm-{FormName}",
defaults: new { controller = "ProductForm", action = "Index", id = UrlParameter.Optional }
);
}
Correct implementation:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "Product",
url: "Admin/ProductForm-{FormName}",
defaults: new { controller = "ProductForm", action = "Index", id = UrlParameter.Optional }
);
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new {Controller="Home", action = "Index", id = UrlParameter.Optional }
);
}