您好,堆栈溢出!
我正在使用客户端Blazor,偶然发现了有关使用字符串和点(。)进行路由的棘手部分。我希望从管理员控制页面路由到具有以下路由的页面:@page "/ManageGradingExamResults/{StudentEmail}"
。我用以下请求进行了测试:https://localhost/ManageGradingExamResults/[email protected]
,但找不到。如果将剃须刀路径更改为@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/[email protected]
剃刀页面路线:
@page "/**ManageGradingExamResults/{StudentEmail}"
@page "/ManageGradingExamResults"
我的变量:
@code
{
[Parameter]
public string StudentEmail { get; set; }
...
From the documentation you have shared:
[在Blazor Server应用程序中,_Host.cshtml中的默认路由为/(@page“ /”)。包含点(。)的请求URL与默认路由不匹配,因为该URL似乎是在请求文件。 Blazor应用针对不存在的静态文件返回404-Not Found响应。要使用包含点的路由,请使用以下路由模板配置_Host.cshtml:
@page "/{**path}"
在链接生成期间,路由系统对用双星号(**)通用参数(例如,{** myparametername})捕获的值进行编码(正斜杠除外)>
所以这应该为您解决问题
@page "/ManageGradingExamResults/{**StudentEmail}"