Skip to content

Retrieving Route Data from Virtual Path in MVC Router #3427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vezaynk opened this issue Aug 13, 2018 · 1 comment
Closed

Retrieving Route Data from Virtual Path in MVC Router #3427

vezaynk opened this issue Aug 13, 2018 · 1 comment

Comments

@vezaynk
Copy link

vezaynk commented Aug 13, 2018

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.

@Eilon
Copy link
Contributor

Eilon commented Sep 28, 2018

This issue was moved to aspnet/Routing#823

@dotnet dotnet locked and limited conversation to collaborators Sep 28, 2018
@Eilon Eilon closed this as completed Sep 28, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants