c# - 如何实现自定义 RazorViewEngine 以在非标准位置查找 View ?

标签 c# asp.net-mvc razor

我正在考虑实现自定义 RazorViewEngine。基本上我有两个具有相同代码库的站点。不同之处在于它们看起来不同。我想覆盖标准 View 引擎,使 MVC 在两个不同的位置查看其 View 、布局等。一个用于公司 A,另一个用于公司 B。公司 A 将包含主视图,公司 B 的 View 将覆盖这些主视图。因此,我希望 View 引擎在位置 B 中查找 View 、布局、主视图或部分 View (如果找到)然后返回,如果找不到,我希望它默认使用公司 A 的 View 作为默认 View 。显然公司 A 只会查看它自己的文件夹。

进入问题的关键: 我找到了这个网站:http://www.aspnetwiki.com/mvc-3-razor:extending-the-view-engine

第一个问题,这是实现这一目标的最佳方式吗?

其次,我是否需要覆盖 CreatePartialCreateViewFindPartialFindView 方法?

更新

好的,我自己想出了第二个问题,我想覆盖的方法是 CreateViewCreatePartialView 因为此时它已经构建了 View 字符串,我可以摆弄它。

最佳答案

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

感谢@Adriano 提供的答案和指导,但最终我认为这种方法更符合我的需求。下面的方法允许我保留标准功能,但可以创建一个新的更高优先级的 View 位置进行搜索。

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());
}

关于c# - 如何实现自定义 RazorViewEngine 以在非标准位置查找 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9838766/

相关文章:

c# - 没有 I/O 的等待开销是多少?

c# - 将两个列表返回到 View c# mvc

c# - 如何最多显示两位数但可以显示更少?

razor - Visual Studio 2013 不打开 *.cshtml 文件

c# - 如何在存在打开的连接时恢复 SQLite 数据库?

c# - Kendo MVC 在 Update 调用上触发 Create 方法

c# - 通过另一个线程将局部变量修改为 null,这怎么可能

c# - mefcontrib拦截目录导出错误

c# - 从 IViewComponentHelper 扩展方法中调用 Component.InvokeAsync()

jquery - 从下拉列表助手中删除一个元素