I can't believe how frustratingly difficult I'm finding such a simple task.
I want to create a link with a page
query argument.
I tried the following:
<a asp-page="Index" asp-route-page="10">Character Classifications</a>
And here's the link that is generated.
<a href="/">Character Classifications</a>
I've been Googling for an hour. Can anyone tell me the Razor Pages way to create a link with a query argument?
Also, is there a shortcut if I'm linking to the current page?
page
is a reserved routing name:
The following keywords are reserved names and can't be used as route names or parameters:
- action
- area
- controller
- handler
- page
If you change asp-route-page
to something that's not on the list above, e.g. asp-route-pageNumber
, it'll work.
Kirk's answer is straight to the point but I like to elaborate a bit on why it is not possible and the reason behind the reserved words.
The official reason - as taken from the github issue - goes like this:
This decision was made in 2007 during the initial release of MVC. Route data is something that user code can see, inspect and modify. The 'keys' for route values like are frequently typed in user-provided routes and calls to apis like Url.Route and Url.Action. It's important that any 'key' also be a valid C# identifier to make it easy to use with the APIs that accept an anonymously typed object. We made a decision not to mangle page because it would be esoteric and weird compared to the other route values that are straightforward.
I think one can debate if using "page" as route is esotoric - I personally don't think so - but maybe that's just me.
Since several other people complained about the reserved words problem, especially the fact that you get no warning or sensible error message an github issue was opened to remedy this problem with an analyzer:
Create an Analyzer to prevent usage of reserved keywords in code #4930
Sadly until today (August 2019) the issue has not been closed.
There is some discussion to rework the internals for .NET 3.0 so that there is no need for reserved keywords anymore when it comes to routing.
But as of today - take care you don't use any of the reserved words for your routing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With