.net Core 2.2 - Generate RouteData from Url path
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!
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);
@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!
@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
I also would like to do this. How can we get a collection of routers in the new route system?
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 ?
I've added a new singleton service that can be used to parse a URL path given an endpoint/route name. See https://github.com/aspnet/AspNetCore/pull/9728 for details. Coming soon to official docs.
That's brilliant thanks! I might be being a bit thick here but could you link to the implementation?
Oops it's https://github.com/aspnet/AspNetCore/pull/9728 - fixed my link, thanks