c# - 在 ASP.NET Core Razor Pages 中重命名页面/共享目录

标签 c# asp.net-core razor-pages asp.net-core-5.0

我正在使用 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/

相关文章:

c# - Web 浏览器 Hook 和自动化

asp.net-core - 如何在 aspnetcore razor View 页面 (.cshtml) 中启用#nullable

c# - 此查询是否存在 SQL 注入(inject)风险?如果是这样,我该如何避免呢?

c# - 将当前用户包含在 Controller 对服务层的每次调用中

c# - 是否可以在 .NET Core 中将 gRPC 与 HTTP/1.1 一起使用?

asp.net - Swagger 不同 namespace 中具有相同名称的不同类不起作用

routes - 可选择通过 ASP.NET Core 1.0 应用程序中的 url/route 覆盖请求区域性

c# - ASP.NET Core 2.0 Razor Pages 使用 AddMvcCore() 而不是 AddMvc()

azure-ad-b2c - 使用模板中的 services.AddSignIn(Configuration, 'AddSignIn') 时,“IServiceCollection”不包含 "AzureAdB2C"的定义

c# - 将系列添加到 Excel 图表异常