在 asp.net core mvc 中,我为 View 指定了多个位置。因此,如果某个位置缺少 View ,则会从另一位置获取 View 。
我有一个带有 asp.net core mvc 的 CMS,其中 Controller 和 View 位于已编译的库中。如果我不想使用库中的 View ,很容易添加一个新 View ,例如/Views/ControllerName/Index.cshtml
然后应用程序将使用此 View 而不是从库中获取。
如何在 Razor 页面中完成此操作?基本上,我希望有一个选项可以通过在某个位置添加 .cshtml 文件来覆盖任何页面的 Razor View 。
最佳答案
使用 IViewLocationExpander
,在默认位置之前添加自定义位置。
下面是一个查看“Override”文件夹的示例:
public class MyViewLocationExpander : IViewLocationExpander
{
public IEnumerable<string> ExpandViewLocations(
ViewLocationExpanderContext context,
IEnumerable<string> viewLocations)
{
// {2} = Area
// {1} = Controller
// {0} = Action
var locations = new string[] { "/Views/Override/{2}/{1}/{0}.cshtml" };
return locations.Union(viewLocations);
}
public void PopulateValues(ViewLocationExpanderContext context)
{
}
}
然后在您的 Startup
类中注册它:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<RazorViewEngineOptions>(options =>
{
options.ViewLocationExpanders.Add(new MyViewLocationExpander());
});
}
关于asp.net-core - 覆盖 asp.net core razor 页面中的 razor View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46230400/