Razor Pages 有没有办法解决这个问题:
我有一个用户属于两家公司:这些公司的 ID 为 1 和 2
我的 Razor Pages 应用程序有以下页面:
Pages/Index.cshtml
Pages/Account/Login.cshtml
Pages/Account/Logout.cshtml
Pages/Dashboard/Index.cshtml
Pages/Employees/Index.cshtml
我想要实现的是:
对于此页面,我想要常规路线
Pages/Index.cshtml
Pages/Account/Login.cshtml
Pages/Account/Logout.cshtml
对于此页面,我希望路线为/[COMPANY_ID]/Dashboard
或/[COMPANY_ID]/Employees
Pages/Dashboard/Index.cshtml
Pages/Employees/Index.cshtml
在Pages/Index.cshtml
用户可以选择使用 id 1 或 2 的公司仪表板。
关于Pages/Dashboard/Index.cshtml
我要创建的链接Pages/Employees/Index.cshtml
,如何确保[COMPANY_ID]
自动添加权限?
谢谢
对于页面
Pages/Dashboard/Index.cshtml
和Pages/Employees/Index.cshtml
,我希望路线是/[COMPANY_ID]/Dashboard
或/[COMPANY_ID]/Employees
在 Pages/Dashboard/Index.cshtml 我想创建一个指向 Pages/Employees/Index.cshtml 的链接,如何确保自动添加正确的 [COMPANY_ID] ?
要实现上述要求,您可以尝试:
添加和配置到您的页面的路由
services
.AddRazorPages()
.AddRazorPagesOptions(options =>
{
options.Conventions.AddPageRoute("/Dashboard/Index", "/{id:int}/Dashboard/Index");
options.Conventions.AddPageRoute("/Employees/Index", "/{id:int}/Employees/Index");
});
在Dashboard/Index.cshtml
页面
@page
@model xxx.Pages.Dashboard.IndexModel
@{
var hasId = HttpContext.Request.RouteValues.TryGetValue("id", out object id);
}
<h1>Dashboard Index</h1>
<a href=@(@hasId ? $"/{id}/Employees/Index" : "/Employees/Index")>Employees Index</a>
测试结果
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句