在 Razor Pages 项目中,我必须覆盖 ASP.NET Identity 的默认 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 some folder convention? Also I don't want to have `/auth/{page}` URLs still working (which is a problem with the current approach).
您可以通过自定义页面路由操作约定来实现此目的。
您可以像下面这样更改您的代码。
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);
}
});
});