剃刀页面链接忽略路由参数 [英] Razor page link ignores route argument

查看:68
本文介绍了剃刀页面链接忽略路由参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不敢相信找到如此简单的任务有多么令人沮丧.

我想创建一个带有 page 查询参数的链接.

我尝试了以下操作:

 < a asp-page =索引" asp-route-page ="10">字符分类</a> 

这是生成的链接.

 < a href ="/">字符分类</a> 

我已经谷歌搜索了一个小时.谁能告诉我Razor Pages创建带有查询参数的链接的方法吗?

如果我链接到当前页面,还有快捷方式吗?

解决方案

柯克的答案很直截了当,但我想详细说明为什么这是不可能的,以及保留字后面的原因.

官方原因-摘自github问题-如下:

该决定是在2007年MVC的初始发行版中做出的.路线数据是用户代码可以查看,检查和修改的内容.路线值之类的键"经常在用户提供的框中输入路由和对诸如Url.Route和Url.Action之类的api的调用.这一点很重要任何键"也是有效的C#标识符,以使其易于使用带有接受匿名类型对象的API.我们做了一个决定不捣乱页面,因为它太深奥了与其他简单明了的路线值相比.

我认为有人可以争论是否使用页面"作为路线是深奥的-我个人并不这么认为-但这也许就是我.

由于其他一些人抱怨保留字问题,尤其是您没有收到警告或明智的错误消息,因此打开了github问题以使用分析器解决此问题:

创建分析器,以防止在代码#4930中使用保留关键字

不幸的是,直到今天(2019年8月),该问题尚未解决.

现在有一些讨论可以对.NET 3.0的内部进行重新处理,以便在路由时不再需要保留关键字.

但是从今天开始-请注意,请勿在路由中使用任何保留字.

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?

解决方案

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.

这篇关于剃刀页面链接忽略路由参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆