简体   繁体   English

Razor 带有前缀 id 的页面路由

[英]Razor Pages routing with a prefix id

Is there a way with Razor Pages to solve this: Razor Pages 有没有办法解决这个问题:

I have a user that is part of two companies: These companies have id 1 and 2我有一个用户属于两家公司:这些公司的 id 为 1 和 2

My Razor Pages app has these pages:我的 Razor Pages 应用程序具有以下页面:

Pages/Index.cshtml

Pages/Account/Login.cshtml
Pages/Account/Logout.cshtml

Pages/Dashboard/Index.cshtml
Pages/Employees/Index.cshtml

What I want to achieve is this:我想要实现的是:

For this pages, I want to have the regular routes对于这个页面,我想要常规路线

Pages/Index.cshtml

Pages/Account/Login.cshtml
Pages/Account/Logout.cshtml

For this pages, I want the route to be /[COMPANY_ID]/Dashboard or /[COMPANY_ID]/Employees对于此页面,我希望路线为/[COMPANY_ID]/Dashboard/[COMPANY_ID]/Employees

Pages/Dashboard/Index.cshtml
Pages/Employees/Index.cshtml

On the Pages/Index.cshtml the user can chose to go the dashboard of the company with id 1 or 2.Pages/Index.cshtml上,用户可以选择 go 公司的仪表板,ID 为 1 或 2。

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?Pages/Dashboard/Index.cshtml我想创建一个指向Pages/Employees/Index.cshtml的链接,如何确保自动添加正确的[COMPANY_ID]

Thanks谢谢

For pages Pages/Dashboard/Index.cshtml and Pages/Employees/Index.cshtml , I want the route to be /[COMPANY_ID]/Dashboard or /[COMPANY_ID]/Employees对于页面Pages/Dashboard/Index.cshtmlPages/Employees/Index.cshtml ,我希望路由是/[COMPANY_ID]/Dashboard/[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?在 Pages/Dashboard/Index.cshtml 我想创建一个指向 Pages/Employees/Index.cshtml 的链接,如何确保自动添加正确的 [COMPANY_ID]?

To achieve the above requirement, you can try:要达到上述要求,可以尝试:

add and configure route to your page(s)添加和配置到您的页面的路由

services
    .AddRazorPages()
    .AddRazorPagesOptions(options =>
    {
        options.Conventions.AddPageRoute("/Dashboard/Index", "/{id:int}/Dashboard/Index");
        options.Conventions.AddPageRoute("/Employees/Index", "/{id:int}/Employees/Index");
    });

in Dashboard/Index.cshtml pageDashboard/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>

Test Result测试结果

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM