TL DR
I need a way to programmatically select which RoutePrefix is selected when creating URLs based on user properties in my MVC application
Not TL; DR
I have an MVC 4 application (with AttributeRouting NuGet package)
Due to the requirements of the hosting environment, I have to have two routes for many of my actions, so that the hosting environment can have different access permissions.
I solve this by decorating my controller with [RoutePrefix("full")] [RoutePrefix("lite)] . Which allows you to access each action method via / full / {action} and / lite / {action}.
This works great.
[RoutePrefix("full")] [RoutePrefix("lite")] public class ResultsController : BaseController {
However, each user should use only full or lite in their URLs, which is determined by some properties of this user.
Obviously, when I use RedirectToAction() or @Html.ActionLink() , it will simply select the first available route and not save the “correct” prefix.
I decided that I could override the RedirectToAction() method, and also add my own version of @Html.ActionLink() methods.
This will work, but it will require some kind of nasty code to create the urls, because all I get is a string representing the action and controllers, but not the reflected types. There may also be route attributes, for example, in my example, so I will have to replicate a large number of MVCs embedded in the code.
Is there a better solution to the problem I'm trying to solve?