将子文件夹路由到根 URL
Routing subfolder to root URLs
在 Razor 页面项目中,我必须覆盖 ASP.NET 身份的默认 UI。除了特定的页面要求,我不想要像“/Identity/Account/Whatever”这样的 url - 我的页面将是 /login、/logout 等
但是我仍然想将身份验证页面分组在一个子文件夹中(不要污染顶级文件夹),而是从“根”URL 提供它们。
...
Pages
/Auth
Login.cshtml -> /login
Logout.cshtml -> /logout
ResetPassword.cshtml -> /resetpassword
...
Index.cshtml
Privacy.cshtml
...
目前,为了实现这一点,我的 Startup.cs
中有以下代码
services.AddRazorPages(options =>
{
options.Conventions.AddPageRoute("/Auth/Login", "login");
options.Conventions.AddPageRoute("/Auth/Logout", "logout");
options.Conventions.AddPageRoute("/Auth/ResetPassword", "resetpassword");
...
});
Can this be achieved with folder convention? Also I don't want to have /auth/{page} URLs still working (which is a problem with the current approach).
您可以通过自定义实现此目的 Page route action convention。
您可以像下面这样更改您的代码。
services.AddRazorPages(options =>
{
options.Conventions.AddPageRoute("/Auth/Login", "login");
options.Conventions.AddPageRoute("/Auth/Logout", "logout");
options.Conventions.AddPageRoute("/Auth/ResetPassword", "resetpassword");
options.Conventions.AddFolderRouteModelConvention("/", model =>
{
var selectorCount = model.Selectors.Count;
for (var i = selectorCount - 1; i >= 0; i--)
{
var selectorTemplate = model.Selectors[i].AttributeRouteModel.Template;
if (selectorTemplate.StartsWith("Auth"))
model.Selectors.RemoveAt(i);
}
});
});
在 Razor 页面项目中,我必须覆盖 ASP.NET 身份的默认 UI。除了特定的页面要求,我不想要像“/Identity/Account/Whatever”这样的 url - 我的页面将是 /login、/logout 等
但是我仍然想将身份验证页面分组在一个子文件夹中(不要污染顶级文件夹),而是从“根”URL 提供它们。
...
Pages
/Auth
Login.cshtml -> /login
Logout.cshtml -> /logout
ResetPassword.cshtml -> /resetpassword
...
Index.cshtml
Privacy.cshtml
...
目前,为了实现这一点,我的 Startup.cs
中有以下代码services.AddRazorPages(options =>
{
options.Conventions.AddPageRoute("/Auth/Login", "login");
options.Conventions.AddPageRoute("/Auth/Logout", "logout");
options.Conventions.AddPageRoute("/Auth/ResetPassword", "resetpassword");
...
});
Can this be achieved with folder convention? Also I don't want to have /auth/{page} URLs still working (which is a problem with the current approach).
您可以通过自定义实现此目的 Page route action convention。
您可以像下面这样更改您的代码。
services.AddRazorPages(options =>
{
options.Conventions.AddPageRoute("/Auth/Login", "login");
options.Conventions.AddPageRoute("/Auth/Logout", "logout");
options.Conventions.AddPageRoute("/Auth/ResetPassword", "resetpassword");
options.Conventions.AddFolderRouteModelConvention("/", model =>
{
var selectorCount = model.Selectors.Count;
for (var i = selectorCount - 1; i >= 0; i--)
{
var selectorTemplate = model.Selectors[i].AttributeRouteModel.Template;
if (selectorTemplate.StartsWith("Auth"))
model.Selectors.RemoveAt(i);
}
});
});