带有前缀 id 的 Razor Pages 路由
Razor Pages routing with a prefix id
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
上我想创建一个link到Pages/Employees/Index.cshtml
,如何确保自动添加正确的[COMPANY_ID]
?
谢谢
For pages Pages/Dashboard/Index.cshtml
and Pages/Employees/Index.cshtml
, I want the route to be /[COMPANY_ID]/Dashboard
or /[COMPANY_ID]/Employees
On the Pages/Dashboard/Index.cshtml I want to create a link to Pages/Employees/Index.cshtml, how to make sure that the right [COMPANY_ID] is added automatically ?
要达到以上要求,可以尝试:
添加和配置页面路由
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>
测试结果
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
上我想创建一个link到Pages/Employees/Index.cshtml
,如何确保自动添加正确的[COMPANY_ID]
?
谢谢
For pages
Pages/Dashboard/Index.cshtml
andPages/Employees/Index.cshtml
, I want the route to be/[COMPANY_ID]/Dashboard
or/[COMPANY_ID]/Employees
On the Pages/Dashboard/Index.cshtml I want to create a link to Pages/Employees/Index.cshtml, how to make sure that the right [COMPANY_ID] is added automatically ?
要达到以上要求,可以尝试:
添加和配置页面路由
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>
测试结果