ASP MVC folder hierarchy

I have a pretty big ASP MVC application. Instead of having many controllers 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 submissions

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

Is it easy to do?

+3
source share
5 answers

I think you are looking for something like what the “master” says in this post:

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

ViewEngine, , . , global.asax! , global.asax.

+4

, RouteHandler, .

google:

+1

, , Views . ViewEngine, - . ControllerPathViewEngine GitHub.

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;
        }
    }
0
source

Source: https://habr.com/ru/post/1704617/


All Articles