目前我有一个自定义视图引擎,它将检测应用程序配置文件中的设置并相应地将字符串附加到视图文件的末尾。以州为例,如果它检测到佛蒙特州,它将附加"_VT"
到视图文件名的末尾。它工作正常,如果没有"_VT"
找到它默认为没有任何特殊结尾的文件。
在我的 Razor 视图中,我可以使用该Layout = ""
选项为视图指定布局。我的问题是,有没有办法可以像在视图引擎中所做的那样对布局做类似的事情?基本上,如果在配置中检测到 Vermont,则添加"_VT"
到 Layout View 文件名。
当前视图引擎:
public class PlanSpecificRazorViewEngine : RazorViewEngine
{
public PlanSpecificRazorViewEngine()
{
ViewLocationFormats = new[] { "~/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml" };
PartialViewLocationFormats = new[] { "~/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml" };
AreaViewLocationFormats = new[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{1}/Views/Shared/{0}.cshtml" };
AreaPartialViewLocationFormats = new [] {"~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.cshtml" };
}
public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
string Plan = System.Configuration.ConfigurationManager.AppSettings["Plan"];
return base.FindView(controllerContext, viewName + "_" + Plan, masterName, useCache);
}
public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
{
string Plan = System.Configuration.ConfigurationManager.AppSettings["Plan"];
return base.FindPartialView(controllerContext, partialViewName + "_" + Plan, useCache);
}
}