-
Notifications
You must be signed in to change notification settings - Fork 123
Retrieving Route Data from Virtual Path in MVC Router #823
Description
From @knyzorg on Monday, 13 August 2018 20:01:26
I am working on an .NET Core MVC application which requires alternative controller/action names to be allowed. To accomplish this, I am using my own Router on a MapRoute:
app.UseMvc(routes =>
{
routes.Routes.Add(new CustomRouter(routes.DefaultHandler));
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
My custom router observes the requested controller and action, and based on it places a new value into the RouteData in the request:
public async Task RouteAsync(RouteContext context)
{
[...]
if (requestedAction == "fakeAction")
context.RouteData.Values["action"] = "realAction";
However, to determine the value of the requestedAction, I am basically taking the requested path, splitting it and getting the value of it that way. This seems suboptimal.
What I would like to do would look something like this:
var rr = new RouteBuilder(app);
var myRoute = rr.MapRoute(...).Build();
var myRouteData = myRoute.GetRouteData(context);
myRouteData["action"] == "fakeAction";
Another solution to this problem which I would very much enjoy is if I could do the following:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "something",
template: "{controller=Home}/{action=Index}/{id?}");
routes.Routes.Add(new CustomRouter(routes.DefaultHandler));
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
But NOT have my something route actually route anything and only serve as a way to define the RouteData for my CustomRouter.
Is either of these possible? I do not like the idea us uncleanly implementing existing functionality as it is a both a code smell and a potential maintenance difficulty in the future.
Copied from original issue: dotnet/aspnetcore#3427