This one has been throwing me so I figured I’d ask for some help. I’ve got this to work with my “Categories” routes, but for some reason when I’m trying to do the same thing with “Consumers” it’s not working at all. Here is the necessary background info:
Routing (AdminAreaRegistration.cs):
public override void RegisterArea(AreaRegistrationContext context)
{
//matches /Admin/BusinessCategories/MyCategory/children
context.MapRoute(
"ChildCategories",
"Admin/BusinessCategories/{category}/children",
new { controller = "BusinessCategories", action = "ViewChildren" }
);
//matches /Admin/BusinessCategories/MyCategory/edit
context.MapRoute(
"EditCategory",
"Admin/BusinessCategories/{category}/edit",
new { controller = "BusinessCategories", action = "Edit" }
);
// want this to match /Admin/Consumers/JoeBob/details
context.MapRoute(
"ConsumerDetails",
"Admin/Consumers/{alias}/details",
new { controller = "Consumers", action = "Details" }
);
//matches /Admin
//matches /Admin/BusinessCategories
//matches /Admin/BusinessCategories/New
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "AdminHome", action = "Index", id = UrlParameter.Optional },
new { id = @"\d+" }
);
}
Then in my Consumers controller (ConsumersController.cs):
public class ConsumersController : Controller
{
public ActionResult Index()
{
...code...
}
[HttpGet]
public ActionResult Details(string alias)
{
return View(alias);
}
}
Then I am generating my link like so:
@Html.ActionLink(c.Alias, "Details", "Consumers", new { alias = c.Alias }, null)
The problem is that my call to @Html.ActionLink is generating a link like:
/Admin/Consumers/details?alias=JoeBob (which results in a 404, if clicked on)
rather than what I want, which is:
/Admin/Consumers/JoeBob/details
One thing I’ve noticed is if I change alias = c.Alias to id = c.Alias, it produces a URL like:
/Admin/Consumers/details/JoeBob (so it removes the alias part from the query string, but still puts ‘details’ before the alias.)
Any ideas?
Edit: Rebooting IIS solved the issue for me. Not sure why, but it did.
Your routes are configured properly (I tested them). I suspect that you’re getting this misbehavior because the view that contains your @Html.ActionLink() mark-up is outside of your Admin Area. If it is inside your Admin Area, you should be seeing the proper link generated. However, outside of your Admin Area, you need to declare which area you’re targeting like this: