public class CustomRazorViewEngine : BuildManagerViewEngine
{
// Fields
internal static readonly string ViewStartFileName = "_ViewStart";
// Methods
public CustomRazorViewEngine()
: this(null)
{
}
public CustomRazorViewEngine(IViewPageActivator viewPageActivator)
: base(viewPageActivator)
{
var foders = DirectoryHelper.GetViewsChildDir();
var areaDefaultFormats = new List<string>() { "~/Views/{2}/{1}/{0}.cshtml", "~/Views/{2}/Shared/{0}.cshtml" };
var defaultFormats = new List<string>() { "~/Views/zh.cn/{1}/{0}.cshtml", "~/Views/zh.cn/Shared/{0}.cshtml" };
foreach (var item in foders)
{
var area = "~/Views/{2}/{1}/" + item + "/{0}.cshtml";
var def = "~/Views/zh.cn/{1}/" + item + "/{0}.cshtml";
areaDefaultFormats.Add(area);
defaultFormats.Add(def);
}
base.AreaViewLocationFormats = areaDefaultFormats.ToArray();
base.AreaMasterLocationFormats = areaDefaultFormats.ToArray();
base.AreaPartialViewLocationFormats = areaDefaultFormats.ToArray();
base.ViewLocationFormats = defaultFormats.ToArray();
base.MasterLocationFormats = defaultFormats.ToArray();
base.PartialViewLocationFormats = defaultFormats.ToArray();
base.FileExtensions = new string[] { "cshtml" };
}
protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
string layoutPath = null;
bool runViewStartPages = false;
IEnumerable<string> fileExtensions = base.FileExtensions;
return new RazorView(controllerContext, partialPath, layoutPath, runViewStartPages, fileExtensions, base.ViewPageActivator);
}
protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
string layoutPath = masterPath;
bool runViewStartPages = true;
IEnumerable<string> fileExtensions = base.FileExtensions;
return new RazorView(controllerContext, viewPath, layoutPath, runViewStartPages, fileExtensions, base.ViewPageActivator);
}
}
问题:
为什么var defaultFormats = new List<string>() { "~/Views/zh.cn/{1}/{0}.cshtml", "~/Views/zh.cn/Shared/{0}.cshtml" }; 中的zh.cn不能像areaDefaultFormats 中的一样用{2}来进行格式化呢?以及我该怎么动态去调用每个区域的视图呢(比如说我现在要激活AreaViewLocationFormats),换句话说我怎么去动态的调用en.us和zh.cn两个区域下的视图?
你试试{{{2}}}
呵呵
就是不得行啊?怎么解决呢