Skip to content

.net Core 2.2 - Generate RouteData from Url path #4597

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
mgolois opened this issue Dec 12, 2018 · 8 comments
Closed

.net Core 2.2 - Generate RouteData from Url path #4597

mgolois opened this issue Dec 12, 2018 · 8 comments
Assignees
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates Done This issue has been fixed

Comments

@mgolois
Copy link

mgolois commented Dec 12, 2018

Hello,

I'm looking for documentation on the new endpoint routing system. I'm upgrading from .net core 2.1 to 2.2. I have a scenario where I need to generate the RouteData object from a url path.
I could not get it to work with IUrlHelper.

Thanks!

@MarxITSolutions
Copy link

I am having the same issue after scaffolding the Identity. The callbackUrl always returns null. Page: Register.cshtml.cs
var callbackUrl = Url.Page(
"/Account/ConfirmEmail",
pageHandler: null,
values: new { userId = user.Id, code = code },
protocol: Request.Scheme);

@Eilon Eilon added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Dec 13, 2018
@rynowak
Copy link
Member

rynowak commented Jan 9, 2019

@mgolois - hi, sorry for the delayed response. It's not 100% clear to me what you're trying to do, so I'm guessing.

If what you want is to parse a URL into route values, you can use the TemplateMatcher for that https://github.com/aspnet/AspNetCore/blob/master/src/Http/Routing/src/Template/TemplateMatcher.cs#L10

I'm closing this issue since I believe I answered the question. Feel free to open another issue is this is not what you need.


@MarxITSolutions - if you're still having this problem could you please open a new issue and include a minimal repro of the problem so that we can investigate? Thanks!

@mgolois
Copy link
Author

mgolois commented Jan 15, 2019

@rynowak , in order to use TemplateMatcher I need to get the teamples, so all route collections.
With the new route system this no longer works:
httpcontext.GetRouteData().Routers.OfType<IRouteCollection>().List();

Basically, after upgrading to 2.2 I would like to use the new routing system, the application has a method that contains a list of strings (urlPaths) and needs to generate RouteData of each urlPaths.
eg:
/Students/GradeBook/Index: area=Students, controller=GradeBook, action=Index
/Home/Index: controller=Home, action=Index

@rynowak rynowak reopened this Jan 16, 2019
@rynowak rynowak added question and removed question labels Feb 20, 2019
@oferns
Copy link

oferns commented Feb 25, 2019

I also would like to do this. How can we get a collection of routers in the new route system?

@oferns
Copy link

oferns commented Feb 26, 2019

Ok. So i dont know about @rynowak but I use conventional route patterns. I know everyone loves attribute routing but I don't see the appeal but I believe this approach could work with that as well. What I am doing now is after registering the routes in the UseMvc extension method like this

 app.UseMvc(routes => {
                routes.MapRoute(
                    name: "Default",
                    template: "{action}",
                    defaults: new { controller = "Home", action = "Index" }
                );

I then loop the routes collection here and add the route templates to an internal static collection in the startup.cs class. Then I can access all the templates anywhere in the code and match them using the matcher. This allows me to get a collection of route values and a match from a path string like "/Home/Index". I cannot help feeling there should be an easier way...

using Microsoft.AspNetCore.Routing.Template;
internal static List<RouteTemplate> MvcRoutes = new List<RouteTemplate>();

routes.MapRoute(
                    name: "Default",
                    template: "{action}",
                    defaults: new { controller = "Home", action = "Index" }
);

foreach (var route in routes.Routes) {
if (route is Route r) {
      MvcRoutes.Add(r.ParsedTemplate);
   }
}

Then I can access the registered route templates with Startup.MvcRoutes and subsequently use the pattern matcher to parse the path string.

Im a bit rushed at the moment but can I suggest maybe just reverting to populating RouteData.Routes ?

@rynowak rynowak added this to the 3.0.0-preview6 milestone Apr 24, 2019
@rynowak rynowak self-assigned this Apr 24, 2019
rynowak pushed a commit that referenced this issue Apr 24, 2019
Adds functionality to LinkGenerator to parse a URI path given a way to
find an endpoint. This is the replacement for various machinications
using the global route collection and `RouteData.Routers` in earlier
versions.

For now I'm just adding a way to do this using Endpoint Name since it's
a pretty low level feature. Endpoint Name is also very direct, so it
feels good for something like this.

I added this to LinkGenerator because I think it feels like the right
thing do, despite the naming conflict. I don't really want to create a
new top-level service for this.
rynowak pushed a commit that referenced this issue May 1, 2019
Adds functionality to LinkGenerator to parse a URI path given a way to
find an endpoint. This is the replacement for various machinications
using the global route collection and `RouteData.Routers` in earlier
versions.

For now I'm just adding a way to do this using Endpoint Name since it's
a pretty low level feature. Endpoint Name is also very direct, so it
feels good for something like this.

I added this to LinkGenerator because I think it feels like the right
thing do, despite the naming conflict. I don't really want to create a
new top-level service for this.
rynowak pushed a commit that referenced this issue May 2, 2019
Adds functionality to LinkGenerator to parse a URI path given a way to
find an endpoint. This is the replacement for various machinications
using the global route collection and `RouteData.Routers` in earlier
versions.

For now I'm just adding a way to do this using Endpoint Name since it's
a pretty low level feature. Endpoint Name is also very direct, so it
feels good for something like this.

I added this to LinkGenerator because I think it feels like the right
thing do, despite the naming conflict. I don't really want to create a
new top-level service for this.
rynowak pushed a commit that referenced this issue May 3, 2019
Adds functionality to LinkGenerator to parse a URI path given a way to
find an endpoint. This is the replacement for various machinications
using the global route collection and `RouteData.Routers` in earlier
versions.

For now I'm just adding a way to do this using Endpoint Name since it's
a pretty low level feature. Endpoint Name is also very direct, so it
feels good for something like this.

I added this to LinkGenerator because I think it feels like the right
thing do, despite the naming conflict. I don't really want to create a
new top-level service for this.
@rynowak rynowak closed this as completed in 127bc7d May 3, 2019
@rynowak
Copy link
Member

rynowak commented May 3, 2019

I've added a new singleton service that can be used to parse a URL path given an endpoint/route name. See #9728 for details. Coming soon to official docs.

@oferns
Copy link

oferns commented May 3, 2019

That's brilliant thanks! I might be being a bit thick here but could you link to the implementation?

@rynowak
Copy link
Member

rynowak commented May 3, 2019

Oops it's #9728 - fixed my link, thanks

@mkArtakMSFT mkArtakMSFT added Done This issue has been fixed and removed Working labels Jun 2, 2019
@rynowak rynowak mentioned this issue Jun 3, 2019
6 tasks
@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates Done This issue has been fixed
Projects
None yet
Development

No branches or pull requests

6 participants