防止在Razor页面中自动传递URL参数 [英] Prevent automatic passing of URL parameter in Razor Pages
问题描述
我正在为Web应用程序使用.NET Core 2"Razor Pages"格式.它允许您使用顶部的简单指令定义如何将URL映射到变量名称:
I'm using the .NET Core 2 "Razor Pages" format for a web app. It allows you to define how to map the URL to variable names with a simple directive at the top:
@page "{personId:int?}"
例如,上面的代码会将URL /Person/Index/123
映射到 IndexModel.OnGetAsync(int personId)
函数,从而使 personId
的值为123.
The above, for example, would map the URL /Person/Index/123
to the IndexModel.OnGetAsync(int personId)
function so that personId
would have a value of 123.
但是,我注意到,如果页面上存在链接,这些值似乎会自动添加到可能的任何URL中.如果我将以下内容添加到索引"页面:
However, I've noticed that these values seem to be automatically added to any URL they could be, if a link exists on the page. If I add the following to the Index page:
<a asp-page="/Person/Details">Details</a>
…生成的实际HTML是:
…the actual HTML that's generated is:
<a href="/Person/Details/123">Details</a>
…意味着由于索引"和详细信息"页面的顶部都具有相同的指令,因此 personId
的值会在它们之间隐式传递.
…meaning that because both the Index and Details pages have the same directive at the top, the value of personId
is implicitly passed between them.
有什么方法可以防止这种情况,以便 asp-page ="/Person/Details"
可以直接重定向到详细信息"页面,而无需传递 personId
的任何值?
Is there any way to prevent this, so that asp-page="/Person/Details"
would simply redirect to the Details page without any value passed for personId
?
推荐答案
对于 Asp.Net Core
,当前请求的当前路由值被视为链接生成的环境值.
For Asp.Net Core
, the current route values of the current request are considered ambient values for link generation.
对于您的情况, personId
的123将被重新用于< asp-page ="/Person/Details"> Details</a>
链接生成.
For your scenario, 123 for personId
will be reused for <a asp-page="/Person/Details">Details</a>
link generation.
在此网址生成过程中,您可以尝试从 RouteData
From this url generation process, you could try to remove the value from RouteData
public async Task OnGetAsync(int? personId)
{
Person = await _context.Person.ToListAsync();
RouteData.Values.Remove("personId");
}
对于另一种更好的方法,您可以尝试从url生成中删除该值.
For another better way, you could try to remove the value from url generation.
<a asp-page="/Person/Details" asp-route-personId="">Details</a>
这篇关于防止在Razor页面中自动传递URL参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!