Prevent automatic passing of URL parameter in Razo

2019-09-03 12:45发布

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条回答
Rolldiameter
2楼-- · 2019-09-03 13:29

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>
查看更多
登录 后发表回答