我正在尝试实现自定义视图引擎,这将允许我指定如下所示的其他视图路径:
public class CustomViewEngine : RazorViewEngine
{
public CustomViewEngine()
{
ViewLocationFormats = new[]
{
"~/Views/{1}/{0}.cshtml", "~/Views/{1}/{0}.vbhtml",
"~/Views/Shared/{0}.cshtml", "~/Views/Shared/{0}.vbhtml"
};
MasterLocationFormats = new[]
{
"~/Views/{1}/{0}.cshtml", "~/Views/{1}/{0}.vbhtml",
"~/Views/Shared/{0}.cshtml", "~/Views/Shared/{0}.vbhtml"
};
PartialViewLocationFormats = new[]
{
"~/Views/{1}/{0}.cshtml", "~/Views/{1}/{0}.vbhtml",
"~/Views/Shared/{0}.cshtml", "~/Views/Shared/{0}.vbhtml",
"~/Views/Partials/Widgets/{0}.cshtml", "~/Views/Partials/Widgets/{0}.vbhtml"
};
}
}
例如,在Global.asax.cs中有很多源代码如何使用它:Can I specify a custom location to "search for views" in ASP.NET MVC?但是没有任何资料显示使用OWIN时如何注册此引擎。怎么做?
尝试一下:
Startup.cs
using System.Web.Mvc;
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(WebApplication2.Startup))]
namespace WebApplication2
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new CustomViewEngine());
}
}
}