我正在使用 ASP.NET Core 5 Razor Pages。通用模板位于 Pages/Shared
中,但我需要将其重命名为 Pages/Foo
。
如何指示运行时在 Pages/Foo
中查找文件?
我认为在 Startup.ConfigureServices()
中是可能的:
services.AddRazorPages(options => {
options.Conventions.??? // what goes here?
});
最佳答案
用于定位 Razor 页面 View 的路径在 PageViewLocationFormats
中定义,这是 RazorViewEngineOptions
的属性。您可以操纵此集合来自定义这些路径。
这是一个例子:
services.AddRazorPages()
.AddRazorOptions(o =>
{
var indexOfPagesShared = o.PageViewLocationFormats.IndexOf("/Pages/Shared/{0}.cshtml");
o.PageViewLocationFormats.RemoveAt(indexOfPagesShared);
o.PageViewLocationFormats.Insert(indexOfPagesShared, "/Pages/Foo/{0}.cshtml");
});
修改列表的方法有很多,但这个例子展示了如何用 /Pages/Foo
替换现有的 /Pages/Shared
路径。它对根 (/Pages
) 和文件扩展名进行了假设,但这些都是典型的。
请注意,如果您使用区域,还有一个 AreaPageViewLocationFormats
属性(property)。
关于c# - 在 ASP.NET Core Razor Pages 中重命名页面/共享目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66048395/