重命名ASP.NET Core Razor Pages中的Pages/Shared目录 [英] Rename Pages/Shared directory in ASP.NET Core Razor Pages
问题描述
我正在使用ASP.NET Core 5 Razor Pages.常见模板位于 Pages/Shared
中,但是我需要将其重命名为 Pages/Foo
.
I'm using ASP.NET Core 5 Razor Pages. The common templates go in Pages/Shared
, but I need to rename it to Pages/Foo
.
如何指示运行时在 Pages/Foo
中查找文件?
How do I instruct the runtime to look for files in Pages/Foo
?
我认为在 Startup.ConfigureServices()
中是可能的:
services.AddRazorPages(options => {
options.Conventions.??? // what goes here?
});
推荐答案
The paths used to locate Razor Pages views are defined in PageViewLocationFormats
, which is a property of RazorViewEngineOptions
. You can manipulate this collection to customise these paths.
这是一个例子:
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
)和文件扩展名进行了假设,但这是典型的.
There's a tonne of different approaches to modifying the list, but this example shows how to replace the existing /Pages/Shared
path with /Pages/Foo
. It makes assumptions about the root (/Pages
) and the file-extension, but these are typical.
请注意,如果您使用的是区域,那么还会有 AreaPageViewLocationFormats
属性.
Note that if you're using areas, there's also an AreaPageViewLocationFormats
property.
这篇关于重命名ASP.NET Core Razor Pages中的Pages/Shared目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!