栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何实现自定义RazorViewEngine在非标准位置查找视图?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何实现自定义RazorViewEngine在非标准位置查找视图?

好的,最后我选择了一种详细的方法:http :
//weblogs.asp.net/imranbaloch/archive/2011/06/27/view-engine-with-dynamic-
view-
location.aspx

感谢@Adriano的回答和建议,但最终我认为这种方法更适合我的需求。下面的方法使我可以保留标准功能,但可以创建要搜索的更高优先级的新位置。

public class Travel2ViewEngine : RazorViewEngine{    protected BrandNameEnum BrandName;    private string[] _newAreaViewLocations = new string[] {        "~/Areas/{2}/%1Views/{1}/{0}.cshtml",        "~/Areas/{2}/%1Views/{1}/{0}.vbhtml",        "~/Areas/{2}/%1Views//Shared/{0}.cshtml",        "~/Areas/{2}/%1Views//Shared/{0}.vbhtml"    };    private string[] _newAreaMasterLocations = new string[] {        "~/Areas/{2}/%1Views/{1}/{0}.cshtml",        "~/Areas/{2}/%1Views/{1}/{0}.vbhtml",        "~/Areas/{2}/%1Views/Shared/{0}.cshtml",        "~/Areas/{2}/%1Views/Shared/{0}.vbhtml"    };    private string[] _newAreaPartialViewLocations = new string[] {        "~/Areas/{2}/%1Views/{1}/{0}.cshtml",        "~/Areas/{2}/%1Views/{1}/{0}.vbhtml",        "~/Areas/{2}/%1Views/Shared/{0}.cshtml",        "~/Areas/{2}/%1Views/Shared/{0}.vbhtml"    };    private string[] _newViewLocations = new string[] {        "~/%1Views/{1}/{0}.cshtml",        "~/%1Views/{1}/{0}.vbhtml",        "~/%1Views/Shared/{0}.cshtml",        "~/%1Views/Shared/{0}.vbhtml"    };    private string[] _newMasterLocations = new string[] {        "~/%1Views/{1}/{0}.cshtml",        "~/%1Views/{1}/{0}.vbhtml",        "~/%1Views/Shared/{0}.cshtml",        "~/%1Views/Shared/{0}.vbhtml"    };    private string[] _newPartialViewLocations = new string[] {        "~/%1Views/{1}/{0}.cshtml",        "~/%1Views/{1}/{0}.vbhtml",        "~/%1Views/Shared/{0}.cshtml",        "~/%1Views/Shared/{0}.vbhtml"    };    public Travel2ViewEngine()        : base()    {        Enum.TryParse<BrandNameEnum>(Travel2.WebUI.Properties.Settings.Default.BrandName, out BrandName);        AreaViewLocationFormats = AppendLocationFormats(_newAreaViewLocations, AreaViewLocationFormats);        AreaMasterLocationFormats = AppendLocationFormats(_newAreaMasterLocations, AreaMasterLocationFormats);        AreaPartialViewLocationFormats = AppendLocationFormats(_newAreaPartialViewLocations, AreaPartialViewLocationFormats);        ViewLocationFormats = AppendLocationFormats(_newViewLocations, ViewLocationFormats);        MasterLocationFormats = AppendLocationFormats(_newMasterLocations, MasterLocationFormats);        PartialViewLocationFormats = AppendLocationFormats(_newPartialViewLocations, PartialViewLocationFormats);    }    private string[] AppendLocationFormats(string[] newLocations, string[] defaultLocations)    {        List<string> viewLocations = new List<string>();        viewLocations.AddRange(newLocations);        viewLocations.AddRange(defaultLocations);        return viewLocations.ToArray();    }    protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)    {        return base.CreateView(controllerContext, viewPath.Replace("%1", BrandName.ToString()), masterPath);    }    protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)    {        return base.CreatePartialView(controllerContext, partialPath.Replace("%1", BrandName.ToString()));    }    protected override bool FileExists(ControllerContext controllerContext, string virtualPath)    {        return base.FileExists(controllerContext, virtualPath.Replace("%1", BrandName.ToString()));    }}

然后在Gloabal.asax中注册

protected void Application_Start(object sender, EventArgs e){    RegisterRoutes(RouteTable.Routes);    //Register our customer view engine to control T2 and TBag views and over ridding    ViewEngines.Engines.Clear();    ViewEngines.Engines.Add(new Travel2ViewEngine());}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/573276.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号