1

I have an ASP.NET Core 9.0 Razor pages application.

In my application I generate navigation URLs using Url.Page method.

However if I'm currently on a page which has additional parameters (for example /Report/2025/10) then the Url.Page("Report") will return /Report/2025/10.

I would like to get the 'clean' URL: (/Report) without current parameters.

Is this possible? I've tried setting the values parameter to null or empty object (new {}) but it seems to be ignoring this.

Note: my parameters are optional so the page will work even without them.

2 Answers 2

2

One way would be to explicitly clean route parameters:

@Url.Page("/Report", new { year = (int?)null, month = (int?)null })

assuming you have specified

@page "{year?}/{month?}"

at the top of your page.

Sign up to request clarification or add additional context in comments.

1 Comment

I will try it out but I think the LinkGenerator answer might be a better solution.
1

You can also use LinkGenerator, although it works slightly differently. For example, the slash is mandatory:

@page "/report/{year:int?}/{month?}"
@inject LinkGenerator Link
@model MyCoolReportModel

@Link.GetPathByPage("/MyCoolReport") @* /report *@

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.