pass querystring to AddPageRoute in Asp.net core razor pages

Viewed 670

how to pass querystring to AddPageRoute in Asp.net core razor pages ?

for example

options.Conventions.AddPageRoute("/WritingList?typeID=1", "Blog");

when I add querystring , routing does not work.

I want to have these routes:

  1. domain.com/blog
  2. domain.com/news
  3. domain.com/mobile
  4. domain.com/laptop

Number 1,2 have the same structure so they should be routed to WritingList page. Number 3,4 have the same structure so they should be routed to ProductList page but I can't access querystring or route value in these pages

2 Answers

Use AddPageRoute to configure a route to a page at the specified page path.Generated links to the page use your specified route.

AddPageRoute uses AddPageRouteModelConvention to establish the route.

Example:

options.Conventions.AddPageRoute("/Contact", "TheContactPage/{text?}");

The sample app creates a route to /TheContactPage for Contact.cshtml.he Contact page can also be reached at /Contact via its default route.

The sample app's custom route to the Contact page allows for an optional text route segment ({text?}).

The page also includes this optional segment in its @page directive in case the visitor accesses the page at its /Contact route:

@page "{text?}"
@model ContactModel
@{
    ViewData["Title"] = "Contact";
}

<h1>@ViewData["Title"]</h1>
<h2>@Model.Message</h2>

<address>
    One Microsoft Way<br>
    Redmond, WA 98052-6399<br>
    <abbr title="Phone">P:</abbr>
    425.555.0100
</address>

<address>
    <strong>Support:</strong> <a href="mailto:Support@example.com">Support@example.com</a><br>
    <strong>Marketing:</strong> <a href="mailto:Marketing@example.com">Marketing@example.com</a>
</address>

<p>@Model.RouteDataTextTemplateValue</p>

Visit the Contact page at either its ordinary route, /Contact, or the custom route, /TheContactPage.

If you supply an additional text route segment, the page shows the HTML-encoded segment that you provide:

enter image description here

More usage can read this article:https://www.learnrazorpages.com/razor-pages/routing

The AddRoutePage method for your first two URLS are

AddPageRoute("/WritingList", "blog/{val?}");
AddPageRoute("/WritingList", "news/{val?}");

In those pages, you can obtain a route value by binding to a public property in your PageModel named Val, with SupportsGet set to true:

[BindProperty(SupportsGet=true)]
public int? Val {get;set;}

Or by referencing RouteData.Values["val"].

If you prefer to work with query string, you can omit the route parameter from the URL:

AddPageRoute("/WritingList", "blog");
AddPageRoute("/WritingList", "news");

Then you can access the query string value in the PageModel via Request.Query e.g.:

var val = Request.Query["val"];

See here for more information on routing and binding request values:

https://www.learnrazorpages.com/razor-pages/routing
https://www.learnrazorpages.com/razor-pages/model-binding#binding-data-from-get-requests

Related