I’d like to be able to force Url.Action to always add one of my route values to the URL. Basically, I need to be able to add a customer ID in the URL. My routes look like this:
app.MapControllerRoute(
name: "Areas",
pattern: "{customerid:int}/{area:exists}/{controller}/{action=Index}");
app.MapControllerRoute(
name: "default",
pattern: "{customerid:int}/{controller=Home}/{action=Index}/{id?}");
I’m not concerned with exposing the Customer ID since it will always be protected against the authenticated user. These routes will allow a user to view/manage different customers across multiple tabs.
If i navigate to /99/Admin/User, I can correctly read RouteData.Values["customerid"]
. If I use @Url.Action("Index", "Role")
in the view, it correctly renders the URL “/99/Admin/Role”. Since I’m not jumping across areas, it’s smart enough to pass along the customerid route value. However, what if I want to leave to an empty area? For example, using @Url.Action("Index", "Home", new { area = "" })
. Of course, this technically correctly renders /Home/Index
, but is it possible to have it “remember” and use the customerid route value across areas to render /99/Home/Index
?
I know I can use @Url.Action("Index", "Home", new { area = "", customerid = ViewContext.RouteData.Values["customerid"] })
to get the proper path. Or even just create an extension/overload to UrlHelper to sniff the customerid route value. I’m fine with either. Unfortunately, this is an improvement to a large existing application, so making sure to hit every single Url.Action
or Html.ActionLink
is going to be a daunting task. Albeit, one I think I might have to endure. I was just hoping somebody may know of a way to create a “sticky” route of {domain}/{customerid}/...
.