Redirecting to URL with root-value and root-slug in Razor Pages

Jush Stone 21 Reputation points
2022-12-05T15:05:20.017+00:00

How can I redirect to URL like this in ASP.NET Core Razor Pages:

http://localhost:5001/productCategory/cars?page=1

What I did:
1- Define <a> tag in first page:
<a asp-page="./ProductCategory" asp-route-id="@category.Slug" asp-route-page="1">@category.Name</a>

2- Get root value and slug in second page view:
@Anonymous "{id}?page={page}"

3- Get parameters in second page model:
public void OnGet(string id, int page) {}

result: The slug parameter (page) is not received and so getting an error in page landing. What should I do?
I guess I have to define the routes in the Startup.cs file. Will the AddPageRoute() method help me? If yes, how?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,158 questions
0 comments No comments
{count} votes

Accepted answer
  1. Chen Li - MSFT 1,221 Reputation points
    2022-12-06T02:38:04.553+00:00

    Hi @Jush Stone ,

    You can't use asp-route-page to pass parameters, it will conflict with asp-page, resulting in failure to receive parameters.

    Try changing the parameter name. For example:

    In first page:

    <a asp-page="/Privacy" asp-route-id="car" asp-route-pageNumber="1">Test</a>  
    

    In second page model:

    public void OnGet(string id,int pageNumber){ }  
    

    In second page view:

    @page "{id}"  
    

    Test Result:
    267613-image.png

    And the url:
    267526-image.png


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Chen Li


0 additional answers

Sort by: Most helpful