ASP MVC 文件夹层次结构

发布于 2024-07-14 20:20:19 字数 354 浏览 7 评论 0原文

我有一个相当大的 ASP MVC 应用程序。 我宁愿创建一些层次结构,而不是将许多控制器都放在控制器目录中。 所以我可能有类似的事情,

~\Controllers\Security\
~\Controllers\Maintenance\
~\Controllers\Reports\

我也希望能够对视图进行类似的操作,

~\Views\Security\Users\
~\Views\Security\Roles\
~\Views\Maintenance\Customer\
~\Views\Maintenance\Product\

这很容易完成吗?

I have a fairly large ASP MVC application. Instead of having many controllers all in the controller directory I would rather create some hierarchy. So I might have something like

~\Controllers\Security\
~\Controllers\Maintenance\
~\Controllers\Reports\

I would also like to be able to do similar with Views

~\Views\Security\Users\
~\Views\Security\Roles\
~\Views\Maintenance\Customer\
~\Views\Maintenance\Product\

Is this easily done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

绿光 2024-07-21 20:20:20

我认为您正在寻找类似“大师”在这篇文章中所说的内容:

http://haacked.com/archive/0001/01/01/areas-in-aspnetmvc.aspx

基本上,您必须创建一个 ViewEngine 来指定在哪里查找视图。 这是一个相当简单的代码,只是不要忘记将其注册到 global.asax 中! 至于控制器部分,您还必须在 global.asax 中注册新路由。

I think you're looking for something like what the "master" says in this post:

http://haacked.com/archive/0001/01/01/areas-in-aspnetmvc.aspx

Basically you have to create a ViewEngine to specify where to look for the views. It's a fairly simple code, just don't forget to register it in the global.asax! As for the controller part you'll have to register new routes also in the global.asax.

李不 2024-07-21 20:20:20

您正在搜索的概念称为“区域”,如 Phil Haack 在此概述的:
http://haacked.com/archive/2008/11/ 04/areas-in-aspnetmvc.aspx

The concept you're searching for is called "areas", as outlined by Phil Haack here:
http://haacked.com/archive/2008/11/04/areas-in-aspnetmvc.aspx

不语却知心 2024-07-21 20:20:20

我认为你需要编写自己的 RouteHandler,这应该不会太难。

快速谷歌搜索出现:这篇博文详细介绍了它

I would think you'd need to write your own RouteHandler, which shouldn't be too tough.

A quick google search turned up: This blog post detailing it

听不够的曲调 2024-07-21 20:20:20

您是否考虑过切换到功能文件夹。 我已经尝试过(几乎没有修改)并且效果很好。

这篇文章中描述了
http://timgthomas.com/2013/10/feature- folders-in-asp-net-mvc/

代码示例位于 Jimmiy Bogard 的存储库中
https://github.com/jbogard/presentations/tree/master/putyourcontrollersonadietv2

Have you considered to switch to Features Folder. I've tried it (with little modifications) and it works pretty good.

Described in this post
http://timgthomas.com/2013/10/feature-folders-in-asp-net-mvc/

Code examples are in Jimmiy Bogard's repo
https://github.com/jbogard/presentations/tree/master/putyourcontrollersonadietv2

傲鸠 2024-07-21 20:20:20

您真正想要的是让您的视图文件夹层次结构与控制器的命名空间层次结构相匹配。 您可以编写一个自定义 ViewEngine 来相当轻松地完成此操作 - 有关详细信息,请参阅我的 GitHub 上的 ControllerPathViewEngine 项目

我包含了 ControllerPathRazorViewEngine 类的一个片段来概述它的工作原理。 通过拦截 FindView / FindPartialView 方法并将控制器名称替换为文件夹路径(基于控制器命名空间和名称),我们可以让它从主 Views 文件夹内的嵌套文件夹加载视图。

    public class ControllerPathRazorViewEngine : RazorViewEngine
    {
        //... constructors etc.

        public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {
            return FindUsingControllerPath(controllerContext, () => base.FindView(controllerContext, viewName, masterName, useCache));
        }

        public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
        {
            return FindUsingControllerPath(controllerContext, () => base.FindPartialView(controllerContext, partialViewName, useCache));
        }

        private ViewEngineResult FindUsingControllerPath(ControllerContext controllerContext, Func<ViewEngineResult> func)
        {
            string controllerName = controllerContext.RouteData.GetRequiredString("controller");
            string controllerPath = controllerPathResolver.GetPath(controllerContext.Controller.GetType());
            controllerContext.RouteData.Values["controller"] = controllerPath;
            var result = func();
            controllerContext.RouteData.Values["controller"] = controllerName;
            return result;
        }
    }

What you really want here is for your Views folder hierarchy to match the namespace hierarchy of your controllers. You can write a custom ViewEngine to do this fairly easily - see my ControllerPathViewEngine project on GitHub for details.

I've included a snippet of the ControllerPathRazorViewEngine class to outline how it works. By intercepting the FindView / FindPartialView methods and replacing the controller name with a folder path (based on controller namespace and name), we can get it to load views from nested folders within the main Views folder.

    public class ControllerPathRazorViewEngine : RazorViewEngine
    {
        //... constructors etc.

        public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {
            return FindUsingControllerPath(controllerContext, () => base.FindView(controllerContext, viewName, masterName, useCache));
        }

        public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
        {
            return FindUsingControllerPath(controllerContext, () => base.FindPartialView(controllerContext, partialViewName, useCache));
        }

        private ViewEngineResult FindUsingControllerPath(ControllerContext controllerContext, Func<ViewEngineResult> func)
        {
            string controllerName = controllerContext.RouteData.GetRequiredString("controller");
            string controllerPath = controllerPathResolver.GetPath(controllerContext.Controller.GetType());
            controllerContext.RouteData.Values["controller"] = controllerPath;
            var result = func();
            controllerContext.RouteData.Values["controller"] = controllerName;
            return result;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文