我尝试在我的asp.net核心应用程序中自定义引擎视图。所以我添加了这个片段:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddMvc();
var webRootPath = _env.WebRootPath;
string[] nwpath = webRootPath.Split('\\');
string s = "";
for (int i = 0; i < nwpath.Length - 2; i++)
{
if (i == nwpath.Length - 3) s += nwpath[i];
else s += nwpath[i] + "\\";
}
var webSrcPath = s + @"\Application1";
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new CustomViewEngine(webSrcPath));
}
CustomViewEngine
public class CustomViewEngine : System.Web.Mvc.RazorViewEngine
{
public CustomViewEngine(string NewPath)
{
var viewLocations = new[] {
"~/Views/{1}/{0}.cshtml",
"~/Views1/Shared/{0}.cshtml",
Path.Combine(NewPath,"Views/{1}/{0}.cshtml"),
Path.Combine(NewPath,"Views/Shared/{0}.cshtml")
// etc
};
this.PartialViewLocationFormats = viewLocations;
this.ViewLocationFormats = viewLocations;
}
}
当我运行我的应用程序时,我得到了这个着名的例外:
我忘了一些事吗?因为看起来这个功能
Microsoft.AspNetCore.Mvc.Razor.RazorView.GetLayoutPage(ViewContext context,string repeatingFilePath,string layoutPath)
未被覆盖
我该如何解决这个问题?
答案 0 :(得分:1)
您正在使用RazorViewEngine
作为自定义视图引擎的基类,而在注册viewlocations
视图的代码中,您指定了用于WebForm视图引擎的扩展.aspx
,将您的基类更改为WebFormViewEngine
或将所有路径的视图位置更改为.cshtml
所以要么改变你的类继承自WebFormViewEngine
:
public class CustomViewEngine : WebFormViewEngine
{
public CustomViewEngine(string NewPath)
{
var viewLocations = new[] {
"~/Views1/{1}/{0}.aspx",
"~/Views1/{1}/{0}.ascx",
"~/Views1/Shared/{0}.aspx",
"~/Views1/Shared/{0}.ascx" ,
Path.Combine(NewPath,"Views/{1}/{0}.aspx"),
Path.Combine(NewPath,"Views/{1}/{0}.ascx"),
Path.Combine(NewPath,"Views/Shared/{0}.aspx"),
Path.Combine(NewPath,"Views/Shared/{0}.ascx")
// etc
};
或使用razor视图文件扩展名注册路径 csthml :
public class CustomViewEngine : WebFormViewEngine
{
public CustomViewEngine(string NewPath)
{
var viewLocations = new[] {
"~/Views1/{1}/{0}.cshtml",
"~/Views1/Shared/{0}.cshtml",
Path.Combine(NewPath,"Views/{1}/{0}.cshtml"),
Path.Combine(NewPath,"Views/Shared/{0}.cshtml"),
};