MVC级联视图(路由)

时间:2014-03-11 17:16:50

标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing

我即将开始构建一个概念验证应用程序来替换老化的Web表单应用程序。

该应用程序是现成的,基于每个客户进行自定义。现有解决方案包含所有主要UI,以及客户请求它们的自定义页面版本。

首席开发人员构建了一个技巧系统,首先检查客户端目录中是否有页面版本,并且只有在没有找到的情况下才返回基本版本。

即。首先检查: Views \ Client1 \ Home \ Default

如果未找到匹配项,请检查: Views \ Base \ Home \ Default

我的问题是这个;虽然我相当肯定MVC开箱即用,但我无法弄清楚如何开始。你能为我指出正确的方向吗? (适当的教程,谷歌的关键词等)。

全部谢谢!

1 个答案:

答案 0 :(得分:0)

您正在寻找与this Stack Overflow question类似的自定义视图引擎。但是您的用例是不同的,因为它会带来不同的路径,具体取决于哪个租户加载了您的应用程序。

我发现http://weblogs.asp.net/imranbaloch/archive/2011/06/27/view-engine-with-dynamic-view-location.aspx我觉得很适合你的情况。

public class MyRazorViewEngine : RazorViewEngine
{
    public MyRazorViewEngine() : base()
    {
        AreaViewLocationFormats = new[] {
            "~/Areas/{2}/Views/%1/{1}/{0}.cshtml",
            "~/Areas/{2}/Views/%1/{1}/{0}.vbhtml",
            "~/Areas/{2}/Views/%1/Shared/{0}.cshtml",
            "~/Areas/{2}/Views/%1/Shared/{0}.vbhtml"
        };

        AreaMasterLocationFormats = new[] {
            "~/Areas/{2}/Views/%1/{1}/{0}.cshtml",
            "~/Areas/{2}/Views/%1/{1}/{0}.vbhtml",
            "~/Areas/{2}/Views/%1/Shared/{0}.cshtml",
            "~/Areas/{2}/Views/%1/Shared/{0}.vbhtml"
        };

        AreaPartialViewLocationFormats = new[] {
            "~/Areas/{2}/Views/%1/{1}/{0}.cshtml",
            "~/Areas/{2}/Views/%1/{1}/{0}.vbhtml",
            "~/Areas/{2}/Views/%1/Shared/{0}.cshtml",
            "~/Areas/{2}/Views/%1/Shared/{0}.vbhtml"
        };

        ViewLocationFormats = new[] {
            "~/Views/%1/{1}/{0}.cshtml",
            "~/Views/%1/{1}/{0}.vbhtml",
            "~/Views/%1/Shared/{0}.cshtml",
            "~/Views/%1/Shared/{0}.vbhtml"
        };

        MasterLocationFormats = new[] {
            "~/Views/%1/{1}/{0}.cshtml",
            "~/Views/%1/{1}/{0}.vbhtml",
            "~/Views/%1/Shared/{0}.cshtml",
            "~/Views/%1/Shared/{0}.vbhtml"
        };

        PartialViewLocationFormats = new[] {
            "~/Views/%1/{1}/{0}.cshtml",
            "~/Views/%1/{1}/{0}.vbhtml",
            "~/Views/%1/Shared/{0}.cshtml",
            "~/Views/%1/Shared/{0}.vbhtml"
        };
    }

    protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
    {
        var nameSpace = controllerContext.Controller.GetType().Namespace;
        return base.CreatePartialView(controllerContext, partialPath.Replace("%1", nameSpace));
    }

    protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
    {
        var nameSpace = controllerContext.Controller.GetType().Namespace;
        return base.CreateView(controllerContext, viewPath.Replace("%1", nameSpace), masterPath.Replace("%1", nameSpace));
    }

    protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
    {
        var nameSpace = controllerContext.Controller.GetType().Namespace;
        return base.FileExists(controllerContext, virtualPath.Replace("%1", nameSpace));
    }

}

不要将%1替换为命名空间,而是将其替换为您的客户名称。然后将基本路径添加到所有位置的末尾,以确保始终始终找到基本路径。

确保您也将视图引擎添加到ViewEngines.Engines集合中。