文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>ASP.NET MVC 自定义view engine

ASP.NET MVC 自定义view engine

时间:2011-04-21  来源:Rickie


 

1. 先看下webformviewengine的构造函数: public WebFormViewEngine()
{
    base.MasterLocationFormats = new string[] { "~/Views/{1}/{0}.master", "~/Views/Shared/{0}.master" };
    base.ViewLocationFormats = new string[] { "~/Views/{1}/{0}.aspx", "~/Views/{1}/{0}.ascx", "~/Views/Shared/{0}.aspx", "~/Views/Shared/{0}.ascx" };
    base.PartialViewLocationFormats = base.ViewLocationFormats;
}

 

base.ViewLocationFormats 可以看出view page为什么只能写在views文件夹下的原因了。所以我们只需要在新的view engine的构造函数中修改下base.ViewLocationFormats 路径即可。

 

2. 下面是一个自定义的View Engine -- SkinSupportViewEngine,它需要继承WebFormViewEngine。

 

 

    public class SkinSupportViewEngine : WebFormViewEngine
    {
        public SkinSupportViewEngine()
        {
            string[] mastersLocation = new[]
                                           {
                                               string.Format("~/skins/{0}/{0}.master",
                                                             Utils.SkinName)
                                           };
            MasterLocationFormats = this.AddNewLocationFormats(new List<string>(MasterLocationFormats),
                                                               mastersLocation);

            string[] viewsLocation = new[]
                                         {
                                             string.Format("~/skins/{0}/Views/{{1}}/{{0}}.aspx",
                                                           Utils.SkinName),
                                             string.Format("~/skins/{0}/Views/{{1}}/{{0}}.ascx",
                                                           Utils.SkinName)
                                         };
            ViewLocationFormats =
                PartialViewLocationFormats = this.AddNewLocationFormats(new List<string>(ViewLocationFormats),
                                                                        viewsLocation);
        }

......

}

 

3. 最后还需要在Global 文件中注册新的View Engine

            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new SkinSupportViewEngine());

 

 

相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载