View engine depart paths to help organize controllers and view by departement

Topics: ASP.NET MVC
May 31, 2013 at 1:07 AM
Edited May 31, 2013 at 1:13 AM
There is probably a better way to do this but I have been using the mvc 4.5 api and have been having trouble adding a parallel view engine responsible for organizing views in a department directory for each department. That way all views related to that department would be inside that directory. An example would be {departmentdirectory}/{controller}/{action}/{id}.

The goal of my solution is to have a department directory under both the controllers and the views to keep my code organized. I wish I can add this to the api as I think it would be useful but maybe there is a better way?

Is this approach recommended or possible? Although it works after adding it, the action link helpers for home and account are piggy backing off of the new view engine's suggested url path and tacking on my Department name immediately after the views section.

I also tried without success to make DEPT allow for adding the department name through a routing map.

View Engine:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace System.Web.Mvc
{
    /// <summary>
    /// This allows for the DEPT Views to be grouped in a DEPT directory.
    /// Should allow for having multiple data first EF5s in their own directory structure on both the
    /// Controller and Views main folders.
    /// </summary>
    public class DEPTRazorViewEngine : RazorViewEngine
    {
        internal static readonly string ViewStartFileName = "_ViewStart";

        public DEPTRazorViewEngine()
            : this(null)
        {
        }

        public DEPTRazorViewEngine(IViewPageActivator viewPageActivator)
            : base(viewPageActivator)
        {
            AreaViewLocationFormats = new[]
            {
                //"~/Areas/{2}/Views/DEPT/{1}/{0}.cshtml",
                //"~/Areas/{2}/Views/DEPT/Shared/{0}.cshtml",
                "~/Areas/{2}/Views/DEPT/{1}/{0}.cshtml",
                "~/Areas/{2}/Views/DEPT/Shared/{0}.cshtml",
            };
            AreaMasterLocationFormats = new[]
            {
                //"~/Areas/{2}/Views/DEPT/{1}/{0}.cshtml",
                //"~/Areas/{2}/Views/DEPT/Shared/{0}.cshtml",
                "~/Views/DEPT/{1}/{0}.cshtml",
                "~/Views/DEPT/Shared/{0}.cshtml",
            };
            AreaPartialViewLocationFormats = new[]
            {
                //"~/Areas/{2}/Views/DEPT/{1}/{0}.cshtml",
                //"~/Areas/{2}/Views/DEPT/Shared/{0}.cshtml",
                "~/Views/DEPT/{1}/{0}.cshtml",
                "~/Views/DEPT/Shared/{0}.cshtml",
            };

            ViewLocationFormats = new[]
            {
                //"~/Views/DEPT/{1}/{0}.cshtml",
                //"~/Views/DEPT/Shared/{0}.cshtml",
                "~/Views/DEPT/{1}/{0}.cshtml",
                "~/Views/DEPT/Shared/{0}.cshtml",
            };
            MasterLocationFormats = new[]
            {
                //"~/Views/DEPT/{1}/{0}.cshtml",
                //"~/Views/DEPT/Shared/{0}.cshtml",
                "~/Views/DEPT/{1}/{0}.cshtml",
                "~/Views/DEPT/Shared/{0}.cshtml",
            };
            PartialViewLocationFormats = new[]
            {
                //"~/Views/DEPT/{1}/{0}.cshtml",
                //"~/Views/DEPT/Shared/{0}.cshtml",
                "~/Views/DEPT/{1}/{0}.cshtml",
                "~/Views/DEPT/Shared/{0}.cshtml",
            };

            FileExtensions = new[]
            {
                "cshtml",
            };
        }

        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
        {
            IView dView = base.CreateView(controllerContext, viewPath, masterPath);
            return dView;
        }

        protected override IView CreatePartialView(ControllerContext controllerContext, string viewPartialPath)
        {
            IView dView = base.CreatePartialView(controllerContext, viewPartialPath);
            if (controllerContext.RouteData.Values["controller"] == "Home")
            {
                String newViewPartialPath = viewPartialPath.Replace(@"/DEPT", "");
                IView dHomeView = base.CreatePartialView(controllerContext, newViewPartialPath);
                return dHomeView;
            }
            if (controllerContext.RouteData.Values["controller"] == "Account")
            {
                String newViewPartialPath = viewPartialPath.Replace(@"/DEPT", "");
                IView dHomeView = base.CreatePartialView(controllerContext, newViewPartialPath);
                return dHomeView;
            }
            else
            { 
                return dView;
            }
        }
    }
}
MapRoute:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MBNMVC
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("DEPT/Home/{*pathInfo}");
            routes.IgnoreRoute("DEPT/Account/{*pathInfo}");
            routes.IgnoreRoute("DepartmentModel1/{*pathInfo}");  // I only want to call these with the DEPT/DepartmentModel1/Create or other department specific URL 
            routes.IgnoreRoute("DepartmentModel1/{*pathInfo}");   // I only want to call these with the DEPT/DepartmentModel2/Create URL  or other department specific URL

            // used for DEPT namespace
            routes.MapRoute(
                name: "DEPT",
                url: "DEPT/{controller}/{action}/{id}",
                defaults: new { controller = "CompanyAccount", action = "Index", id = UrlParameter.Optional },
                namespaces: new[] { "DEPT" }
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}
May 31, 2013 at 1:26 AM
Maybe areas would be better for organizing by department or schema. My method only works if you add a department directory in the main Views directory.