【问题标题】:How to change or configure directory for View razor cshtml pages如何更改或配置 View razor cshtml 页面的目录
【发布时间】:2020-10-20 12:27:15
【问题描述】:

我试图更新 Razor cshtml 视图页面路径,但它没有采取。要更新视图页面位置,我尝试过 Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    var aiOptions = new Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions
    {
        EnableQuickPulseMetricStream = true
    };
    services.AddMvc();//.WithRazorPagesRoot("/Home/myapp");
    //services.Configure<RazorPagesOptions>(options => options.RootDirectory = "/Home/myapp");
    services.AddApplicationInsightsTelemetry(aiOptions);
    services.AddCors(option =>
    {
        option.AddPolicy("AllowSpecificOrigin", policy => policy.WithOrigins("*"));
        option.AddPolicy("AllowGetMethod", policy => policy.WithMethods("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"));
    });
}

控制器:

public class HomeController : Controller
{
}

当前查看页面路径

Views/Home/Index.cshtml
  Views/Home/AppHome.cshtml

想像这样修改视图路径

Views/Home/myapp/Index.cshtml
 Views/Home/myapp/AppHome.cshtml

但不是在服务配置后它不起作用。如何更改查看页面目录?

谢谢。

【问题讨论】:

标签: c# asp.net asp.net-mvc asp.net-core razor


【解决方案1】:

ASP.NET 框架

要更改视图搜索位置,需要创建一个派生自 RazorViewEngine 的新类,并更改以下一个或多个属性的值:

  • 查看位置格式
  • MasterLocationFormats
  • PartialViewLocationFormats

这些属性中的每一个都是一个字符串数组,使用复合字符串格式化符号表示。对于ViewLocationFormats 属性,默认值为:

~/Views/{1}/{0}.cshtml,
~/Views/{1}/{0}.vbhtml,
~/Views/Shared/{0}.cshtml,
~/Views/Shared/{0}.vbhtml

以下是占位符对应的参数值:

  • {0} 代表视图的名称。
  • {1} 代表控制器的名称。

以下示例显示了如何更改上述所需的视图位置:

public class CustomLocationViewEngine : RazorViewEngine
{
    public CustomLocationViewEngine()
    {
        ViewLocationFormats = new string[] 
        {                
            "~/Views/{1}/myapp/{0}.cshtml",
            "~/Views/{1}/{0}.cshtml"
        };
    }
}

接下来就是在Global.asaxApplication_Start方法中使用ViewEngines.Engines集合注册CustomLocationViewEngine

protected void Application_Start()
{
    for (var i = ViewEngines.Engines.Count - 1; i >= 0; i--)
    {
        if (ViewEngines.Engines[i] is System.Web.Mvc.RazorViewEngine)
        {            
            // Remove the current Razor view engine
            ViewEngines.Engines.RemoveAt(i);
            break;
        }
    }    
    // Add the new customized view engine
    ViewEngines.Engines.Add(new CustomLocationViewEngine());

    //…
}

ASP.NET CORE 3.1

对于 MVC Core 3.1,Startup.cs 中的 ConfigureServices 方法应更新如下:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();
    services.Configure<RazorViewEngineOptions>(o =>
    {
        o.ViewLocationFormats.Clear();
        o.ViewLocationFormats.Add("/Views/{1}/myapp/{0}" + RazorViewEngine.ViewExtension);
        o.ViewLocationFormats.Add("/Views/Shared/myapp/{0}" + RazorViewEngine.ViewExtension);
     });
}

更多信息请见RazorViewEngineOptions.ViewLocationFormats Property

【讨论】:

  • 我有无状态 ASP.NET 核心应用程序在 .NET Framework 上运行。(Web api MVC)...项目中没有 .asax 文件...我可以在 startup.cs 中使用吗文件?
  • @r08 - 对于 ASP.NET MVC CORE 3.1 版本,更新看起来不同。请参阅上面的修复。
猜你喜欢
  • 2021-05-19
  • 2019-02-06
  • 2011-06-23
  • 2019-10-13
  • 2011-09-06
  • 2021-10-18
  • 1970-01-01
  • 2015-04-22
  • 2020-03-14
相关资源
最近更新 更多