We want to migrate a Website from Asp.Net Webforms to Asp.Net Core Webapplication (Razor Pages). We now have readable Urls without visible identifiers (for readability, SEO and to prevent changing Urls after possible database migrations…).
For that i generated dynamically Routes in the Global.Asax at Startup:
RouteTable.Routes.MapPageRoute("myroute1",
"banana/",
"~/content.aspx", false, new RouteValueDictionary { { "id", "4711" }, , { "otherid", "4812" }
RouteTable.Routes.MapPageRoute("myroute2",
"apple/",
"~/content.aspx", false, new RouteValueDictionary { { "id", "4913" }, , { "otherid", "5014" }
That way users could call the content.aspx like this:
https://example.com/banana
https://example.com/apple
In the content.aspx i got the mapped "id" and "otherid" from the RouteValues.
How can i achieve this in Razor Pages?
Now i have a "Content.cshtml" and there i need access to id and otherid. I added this at the top:
@page "/content/{id:int}/{otherid:int}/{title:string}"
The above code allows mit to call Urls like this:
https://example.com/content/4711/4812/banana // still have the ids in it and prefix "content"
Is there a possibility to add all Routes at Startup with fixed Parameters? I have not been able to find anything similar.
Greetings
cpt.oneeye
2
Answers
I found a solution thanks to this thread:
Is there a way to do dynamic routing with Razor Pages?
Another full example can be found here:
https://github.com/dotnet/AspNetCore.Docs/issues/12997
According to my example at the top you first make your own DynamicRouteValueTransformer (see Namespace Microsoft.AspNetCore.Mvc.Routing):
In the Startup.ConfigureServices() you register the new Service:
In the Startup.Configure() you add the NavigationTransformer to Endpoints:
Now when you call a url like the following you will came through the Transformer and you are able to reroute on the fly:
Be aware the Routings of existing Pages are stronger. So if we have a Apple.cshtml the second Url will still be routed to Apple.cshtml and not to Content.cshtml
To have a static url, you can put your routes in startup class. for example:
In this case, you can define the endpoints as much as you want. For example, for your routes, you can write like this:
Or you can use
Attribute Routing
that you can use for each of your controllers. read more about that insorry for my English🙏