Razor page link ignores route argument

痴心易碎 提交于 2019-12-12 10:45:43

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/57400305/razor-page-link-ignores-route-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!