如何使用包含点 (.) 的字符串路由参数在客户端 blazor 中进行路由?

How do I route in client-side blazor with a string route parameter containing dots (.)?

嗨,堆栈溢出

我正在使用客户端 Blazor,并且偶然发现了一个关于使用字符串和点 (.) 进行路由的棘手部分。我希望从管理控制页面路由到具有以下路由的页面:@page "/ManageGradingExamResults/{StudentEmail}"。我用这个请求进行了测试:https://localhost/ManageGradingExamResults/1234@high.school.nz,但我找不到。如果我将剃刀路线更改为 @page "/ManageGradingExamResults/{*StudentEmail}",我最终会出现以下异常:System.InvalidOperationException: Invalid template 'ManageGradingExamResults/{*StudentEmail}'. The character '*' in parameter segment '{*StudentEmail}' is not allowed.。由于 cshtml 页面的相似性,我试过了。

我发现我可以使用整数进行路由,但对字符串就不行了。我也遇到过 this Microsoft 文档解释路由参数并在我的页面路由中建议 ** 。这允许我进入我的页面但是我需要使用?在请求中发送电子邮件之前,页面没有加载我的数据,因为我可以看到它没有将参数拉入我的变量中。 非常感谢任何有关在客户端 Blazor 中进行路由的建议或帮助!

请求代码:

NavigationManager.NavigateTo($"/ManageGradingExamResults/?{student.Email}");

请求URL:

https://localhost/ManageGradingExamResults/?1234@high.school.nz

Razor 页面路由:

@page "/**ManageGradingExamResults/{StudentEmail}"
@page "/ManageGradingExamResults"

我的变量:

@code
{
    [Parameter]
    public string StudentEmail { get; set; }
...

From the documentation you have shared:

In Blazor Server apps, the default route in _Host.cshtml is / (@page "/"). A request URL that contains a dot (.) isn't matched by the default route because the URL appears to request a file. A Blazor app returns a 404 - Not Found response for a static file that doesn't exist. To use routes that contain a dot, configure _Host.cshtml with the following route template: @page "/{**path}"

Based on routing in .net core

During link generation, the routing system encodes the value captured in a double-asterisk (**) catch-all parameter (for example, {**myparametername}) except the forward slashes

所以这应该对你有用

@page "/ManageGradingExamResults/{**StudentEmail}"

Blazor 文档 (https://docs.microsoft.com/en-us/aspnet/core/blazor/fundamentals/routing?view=aspnetcore-5.0#routing-with-urls-that-contain-dots) 建议修改服务器上的回退文件路径。

从 Azure Blob 静态网站或 Amazon S3 托管时,这是不可能的。由于假定点位于 uri 的 最右端 时是一个文件,请考虑移动参数。

URI 最右端带点的参数。这将导致 404,因为 StudentEmail 参数包含一个点并将作为文件路由(假设您无法修改服务器 Startup.Config)。

@page "/ManageGradingExamResults/{StudentEmail}"

移动参数,使其不在 URI 的最右端: 这将正确路由并将 StudentEmail 参数传递给组件。

@page "/GradingExamResults/{StudentEmail}/Manage"