【问题标题】:Rename Pages/Shared directory in ASP.NET Core Razor Pages重命名 ASP.NET Core Razor 页面中的页面/共享目录
【发布时间】:2021-02-04 15:12:42
【问题描述】:

我正在使用 ASP.NET Core 5 Razor 页面。常用模板在Pages/Shared,但我需要重命名为Pages/Foo

如何指示运行时在 Pages/Foo 中查找文件?

我认为Startup.ConfigureServices()是可能的:

services.AddRazorPages(options => {
  options.Conventions.???      // what goes here?
});

【问题讨论】:

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


    【解决方案1】:

    用于定位 Razor 页面视图的路径在 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 属性。

    【讨论】:

    • 在源代码中看起来 like this。遗憾的是它没有正式记录,因为这条魔法线随时可能改变。但它有效,谢谢。
    猜你喜欢
    • 2019-12-15
    • 2019-10-13
    • 2021-04-27
    • 2020-07-09
    • 2020-10-21
    • 2020-08-22
    • 2019-05-22
    • 2020-07-09
    • 2021-10-27
    相关资源
    最近更新 更多