Prevent automatic passing of URL parameter in Razor Pages

五迷三道 提交于 2019-12-24 19:18:05

问题


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?}"

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.

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>

…the actual HTML that's generated is:

<a href="/Person/Details/123">Details</a>

…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.

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?


回答1:


For Asp.Net Core, the current route values of the current request are considered ambient values for link generation.

For your scenario, 123 for personId will be reused for <a asp-page="/Person/Details">Details</a> link generation.

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");
    }

For another better way, you could try to remove the value from url generation.

<a asp-page="/Person/Details" asp-route-personId="">Details</a>


来源:https://stackoverflow.com/questions/52687932/prevent-automatic-passing-of-url-parameter-in-razor-pages

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