c# - 模块化 MVC 应用程序中的布局

标签 c# .net asp.net-mvc razor asp.net-mvc-4

我使用 ninject for MVC 为自己打造了一个模块化框架。

每个模块都可以注册自己的路由并包含自己的 View 。

模块目录(dll 位置):
~/Modules/<module name>/

模块 View 位于内部:
<Module dir>/Views/
它们的排列方式与普通的 mvc 应用程序完全一样,即每个 Controller 一个文件夹和一个共享文件夹。

我想渲染一个带有布局的 View ,但是我希望布局位置由核心框架设置(以便我可以更改主题)。

我有一个 View layout = _layout.cshtml当我运行应用程序时,它返回:

The layout page "_Layout.cshtml" could not be found at the following path: "~/Modules/Module2/Views/Home/_Layout.cshtml".

被调用的 View 在这里~/Modules/Module2/Views/Home/Index.cshtml .但我希望它在另一个位置寻找布局而不是在每个 View 中设置它。无论如何我可以在核心框架中做到这一点吗?请注意,我将其 MasterLocationFormats 也设置为在共享中查看,但它显然没有(我通过在其中放置一个 _layout.cshtml 来测试它)。


自定义 View 引擎:

public NinjectRazorViewEngine(): base()
    {
        ViewLocationFormats = new[] {
            "~/Modules/%1/Views/{1}/{0}.cshtml",
            "~/Modules/%1/Views/{1}/{0}.vbhtml",
            "~/Modules/%1/Views/Shared/{0}.cshtml",
            "~/Modules/%1/Views/Shared/{0}.vbhtml"
        };

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

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

        PartialViewLocationFormats = ViewLocationFormats;
        AreaPartialViewLocationFormats = AreaViewLocationFormats;
    }

    protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
    {
        object moduleName;
        if(controllerContext.RequestContext.RouteData.Values.TryGetValue("module",out moduleName))
            return base.CreatePartialView(controllerContext, partialPath.Replace("%1", (string)moduleName));
        return base.CreatePartialView(controllerContext, partialPath);
    }

    protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
    {
        object moduleName;
        if (controllerContext.RequestContext.RouteData.Values.TryGetValue("module", out moduleName))
            return base.CreateView(controllerContext, viewPath.Replace("%1", (string)moduleName), masterPath.Replace("%1", (string)moduleName));
        return base.CreateView(controllerContext, viewPath, masterPath);
    }

    protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
    {
        object moduleName;
        if (controllerContext.RequestContext.RouteData.Values.TryGetValue("module", out moduleName))
            return base.FileExists(controllerContext, virtualPath.Replace("%1", (string)moduleName));
        return base.FileExists(controllerContext, virtualPath);
    }

最佳答案

这需要大量的工作。

必须对 View 引擎进行更改才能正确公开 FindViewFindPartialView方法。问题中列出的方法是错误的。

这就是 viewEngineClass 的样子

public NinjectRazorViewEngine(): base()
    {
        ViewLocationFormats = new[] {
            "~/Modules/{2}/Views/{1}/{0}.cshtml",
            "~/Modules/{2}/Views/{1}/{0}.vbhtml",
            "~/Modules/{2}/Views/Shared/{0}.cshtml",
            "~/Modules/{2}/Views/Shared/{0}.vbhtml",
            "~/Views/{1}/{0}.cshtml",
            "~/Views/{1}/{0}.vbhtml",
            "~/Views/Shared/{0}.cshtml",
            "~/Views/Shared/{0}.vbhtml"
        };

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

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

        PartialViewLocationFormats = ViewLocationFormats;
        AreaPartialViewLocationFormats = AreaViewLocationFormats;

        //Used to test cache
        //ViewLocationCache = new DefaultViewLocationCache();
    }
    public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
    {
        return FindView(controllerContext, partialViewName, "", useCache);
    }
    public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
    {
        //Implement defualt exceptions
        if(controllerContext == null)
            throw new ArgumentNullException("The controllerContext parameter is null");
        if(string.IsNullOrEmpty(viewName))
            throw new ArgumentException("The viewName parameter is null or empty.");

        //Check cache if specified
        if(useCache && this.ViewLocationCache != null){
            string cachedLocation = ViewLocationCache.GetViewLocation(controllerContext.HttpContext, generateCacheKey(controllerContext, viewName));
            if (!string.IsNullOrEmpty(cachedLocation))
                return new ViewEngineResult(CreateView(controllerContext, cachedLocation, masterName), this);
        }

        //Create arguments for location formatting
        string trimmedViewName = string.Empty;
        if (viewName.EndsWith(".cshtml"))
            trimmedViewName = viewName.Remove(viewName.Length - 7);
        else
            trimmedViewName = viewName;
        object[] args = new object[] { trimmedViewName, controllerContext.RouteData.GetRequiredString("controller"), controllerContext.RouteData.GetRequiredString("module") };

        //Attempt to locate file
        List<string> searchedLocations = new List<string>();
        foreach(string location in ViewLocationFormats){
            string formatedLocation = string.Format(location,args);
            searchedLocations.Add(formatedLocation);
            if (FileExists(controllerContext, formatedLocation))
            {
                //File has been found. Add to cache and return view
                if(this.ViewLocationCache != null)
                    ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, generateCacheKey(controllerContext, viewName), formatedLocation);

                return new ViewEngineResult(CreateView(controllerContext, formatedLocation, masterName), this);
            }
        }

        //Couldnt find view, return searched locations
        return new ViewEngineResult(searchedLocations);
    }
    public string generateCacheKey(ControllerContext controllerContext, string viewName)
    {
        return string.Format("{0}|{1}", controllerContext.RouteData.GetRequiredString("module"), viewName);
    }

然后您需要实现自定义 System.Web.Mvc.WebViewPage<T>像这样:

public abstract class WebViewPage<T> : System.Web.Mvc.WebViewPage<T>
{
    public override string Layout
    {
        get
        {
            return base.Layout;
        }
        set
        {
            NinjectRazorViewEngine viewEngine = new NinjectRazorViewEngine();
            System.Web.Mvc.ViewEngineResult engineResult = viewEngine.FindView(this.ViewContext.Controller.ControllerContext, value, string.Empty, true);
            System.Web.Mvc.RazorView razorView = engineResult.View as System.Web.Mvc.RazorView;
            if (razorView == null)
            {
                string searchedIn = "";
                foreach (string item in engineResult.SearchedLocations)
                {
                    searchedIn += item + "\n";
                }
                throw new HttpException(500, "Could not find views in locations:\n" + searchedIn);
            }
            base.Layout = razorView.ViewPath;
        }
    }
}

希望对您有所帮助:)

关于c# - 模块化 MVC 应用程序中的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13618694/

相关文章:

c# - 检查 Elasticsearch 堆大小

c# - ModelBinder反序列化任意json

c# - 什么时候用短?

javascript - C# Web浏览器以编程方式关闭JS确认框

c# - 在 .net 4.0 wpf 应用程序中使用 .Net 2.0 dll

c# - 如何使用 ActionLink 生成哈希

c# - 如何将列表从 View 发送到 Controller MVC

c# - 在连接到 CRM 的 LINQ 表达式中排除数组

c# - WPF 的控件属性是否存在性能劣势?

.net - vb.net 中的多个 "= new"具有相同的变量。垃圾收集会处理吗?如何处理?