Change the Views location

前端 未结 3 703
粉色の甜心
粉色の甜心 2020-12-18 10:54

I am developing a website in MVC 2.0. I want to change the View folder location in my website. I wanted to keep the views folder inside other folders, When I try to do so i

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-18 11:37

    As an alternative, you can override the view engine locations for a specific controller without affecting the view engines for the other controllers.

    These are some snippets from a product I am developing, but it shows the constructor for one of my controllers, and a view engine I made specificially for controllers that inherit from KBRenderMvcController.

    So any controller based off KBRenderMvcController will also have my view engine.

    However at no point did I clear the view engine collection, which is relevant. Because I wanted the views my product is using to fall back to default locations.

    In short, if you delete \App_plugins\Product\Views\MyView And instead create a \Views\MyView it will still render from \Views\MyView instead.

    Also in the ViewEngine I demonstrate code that determines the type of controller being used and if it's not a target controller I return empty view locations so they don't get used for other controllers.

        #region Constructor
        public KBRenderMvcController()
            : base()
        {
            viewEngine = new KBFrontEndViewEngine();
            if (!this.ViewEngineCollection.Contains(viewEngine))
                this.ViewEngineCollection.Insert(0, viewEngine);
        }
        #endregion
    
    public class KBFrontEndViewEngine : RazorViewEngine
    {
        #region Fields
        private static bool _Initialized = false;
        private static string[] viewLocationFormats = null;
        private static string[] partialViewLocationFormats = null;
        private static string[] viewEngineFileExtensions = new string[] { "cshtml" };
        #endregion
    
        #region Constructor
        public KBFrontEndViewEngine()
        {            
            if (!_Initialized)
            {
                viewLocationFormats = new string[] 
                        { 
                            string.Concat(KBApplicationCore.PluginRelUrl, "/Views/{1}/{0}.cshtml"), 
                            string.Concat(KBApplicationCore.PluginRelUrl, "/Views/Partials/{0}.cshtml") 
                        };
                partialViewLocationFormats = new string[] 
                        { 
                            string.Concat(KBApplicationCore.PluginRelUrl, "/Views/{1}/Partials/_partial{0}.cshtml"), 
                            string.Concat(KBApplicationCore.PluginRelUrl, "/Views/Partials/_partial{0}.cshtml"),
                            string.Concat(KBApplicationCore.PluginRelUrl, "/Views/{1}/Dialogs/_dialog{1}.cshtml"),
                            string.Concat(KBApplicationCore.PluginRelUrl, "/Views/Dialogs/_dialog{1}.cshtml"),
                        };
                _Initialized = true;
            }
            base.ViewLocationFormats = viewLocationFormats;
            base.PartialViewLocationFormats = partialViewLocationFormats;
            base.MasterLocationFormats = viewLocationFormats;
            base.FileExtensions = viewEngineFileExtensions;
        }
        #endregion
    
        #region Methods
        //Don't run on requests that are not for our hijacked controllers
        public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
        {
            Type controllerType = controllerContext.Controller.GetType();
            Type baseType = controllerType.BaseType;
            if ((baseType != null) && (baseType.Name == "KBRenderMvcController`1") || (baseType.Name == "KBFrontEndBaseSurfaceController"))
                return base.FindPartialView(controllerContext, partialViewName, useCache);
            else
                return new ViewEngineResult(new List());
        }
        #endregion
    }
    

提交回复
热议问题