Razor Pages: Dynamic Routing Parameter Name

access_time 17 days ago visibility6 comment 0

In ASP.NET Razor pages, routing component it used to match request URL to a defined routing conventions and then the correspondent page handler will be used to handle the HTTP request and to provide response. Razor pages provide tag helpers to easily create a link based on routing parameters. 

Routing tag example

The following code snippet creates a hyper link based on page model name and routing parameters:

<a asp-area="" asp-page="Article" asp-id="1">Article 1</a>

Assuming the page routing contention is like the following:

options.Conventions.AddPageRoute("/Article", "/article/{id:int}");

The generated hyperlink will be:

<a href="/article/1">Article 1</a>

If the routing convention is like this:

options.Conventions.AddPageRoute("/Article", "/article");

Then the generated hyperlink will be '/article?id=1'. 

Dynamic routing parameters

In some cases, you may want to dynamically pass routing parameters. For example, generate different routing values for different page type:


Or in some scenario, the routing parameters can be dynamic. In the above example, routing parameter id is hardcoded as asp-id="1".  If the parameter value is from a constant variable or other variables or from a database column, you can use asp-route-all-values tag helper attribute:

var idKey = "id";
var parms = new Dictionary<string, string>
                { idKey, "1" }
}; } <a asp-area="" asp-page="Article" asp-all-route-data="parms">Article 1</a>
infoAll routing parameter values need to be string type as asp-all-route-data itself is IDictionary<string,string>. All the additional routing parameters that are not defined in routing conventions will become query string. 
copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

Follow Kontext

Get our latest updates on LinkedIn or Twitter.

Want to publish your article on Kontext?

Learn more

More from Kontext

visibility 3090
thumb_up 0
access_time 6 years ago

In my previous post, I demonstrated how to migrate from ASP.NET Universal Membership Provider to ASP.NET Identity 2.1.0. https://kontext.tech/Blog/DotNetEssential/Archive/2015/1/31/migrate-from-systemwebproviders-to-aspnet-identity-10-and-then-to-210.html Based on the researches, I have ...

visibility 2666
thumb_up 0
access_time 4 months ago

This page summarize information about how to retrieve client and server IP address in ASP.NET core applications.  Client IP address can be retrieved via HttpContext.Connection object. This properties exist in both Razor page model and ASP.NET MVC controller. Property  RemoteIpAddress ...

Modern Web Application - Azure Blob Storage for Uploaded Files
visibility 2479
thumb_up 0
access_time 10 months ago

With cloud platforms like Azure, we can totally separate user content storage from web application storage to decouple components from each other and to make the application easy to scale and deploy. This article provides detailed information with code snippets about how to use Azure server-less product Blob Storage and App Service to enable horizontally scalable web application for users to upload files (BLOBs).