首页 新闻 会员 周边 捐助

mvc3.0 自定义视图引擎

0
悬赏园豆:5 [待解决问题]

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两个区域下的视图?

Haydy的主页 Haydy | 初学一级 | 园豆:177
提问于:2013-03-16 11:53
< >
分享
所有回答(1)
0

你试试{{{2}}}

呵呵

张占岭 | 园豆:464 (菜鸟二级) | 2013-03-18 09:35

就是不得行啊?怎么解决呢

支持(0) 反对(0) Haydy | 园豆:177 (初学一级) | 2013-03-18 10:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册