Is there a way with Razor Pages to solve this :
I have a user that is part of two companies : These companies have id 1 and 2
My Razor Pages app has these 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
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.
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 ?
Thanks
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 ?
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
page
@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