How to change View & partial view default location

后端 未结 2 1236
盖世英雄少女心
盖世英雄少女心 2020-12-30 14:41

i am new in MVC and very curious about to know that how could i change view & partial view location.

we know that view & partial view store in view folder. i

相关标签:
2条回答
  • 2020-12-30 14:59

    If you want to have a special views locations for specific controllers, in your case you want ProductController views to go to MyProduct folder, you need to to override FindView and FindPartialView methods of RazorViewEngine:

    public class MyRazorViewEngine : RazorViewEngine
    {
        public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {
            if (controllerContext.Controller is ProductController)
            {
                string viewPath = "/Views/MyProduct/" + viewName + ".cshtml";
                return base.FindView(controllerContext, viewPath, masterName, useCache);  
            }
    
            return base.FindView(controllerContext, viewName, masterName, useCache);
        }
    
        public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
        {
            if (controllerContext.Controller is ProductController)
            {
                string partialViewPath = "/Views/MyProduct/Partials/" + partialViewName + ".cshtml";
                return base.FindPartialView(controllerContext, partialViewPath, useCache);
            }
    
            return base.FindPartialView(controllerContext, partialViewName, useCache);
        }
    }
    

    And if you maybe want to prepend "My" to every controller views folder, your view engine should look like this

    public class MyRazorViewEngine : RazorViewEngine
    {
        public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {
            string viewPath = "/Views/My" + GetControllerName(controllerContext) + "/" + viewName + ".cshtml";
            return base.FindView(controllerContext, viewPath, masterName, useCache);
        }
    
        public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
        {
            string partialViewPath = "/Views/My" + GetControllerName(controllerContext) + "/Partials/" + partialViewName + ".cshtml";
            return base.FindPartialView(controllerContext, partialViewPath, useCache);
        }
    
        private string GetControllerName(ControllerContext controllerContext)
        {
            return controllerContext.RouteData.Values["controller"].ToString();
        }
    }
    

    And than in your Global.asax

    protected void Application_Start()
    {
        //remove unused view engines, for performance reasons as well
        ViewEngines.Engines.Clear();
        ViewEngines.Engines.Add(new MyRazorViewEngine());
    }
    
    0 讨论(0)
  • 2020-12-30 15:13

    You can modify RazorViewEngine's ViewLocationFormats and PartialViewLocationFormats properties in your Global.asax startup code. Something around the lines below should work:

    protected void Application_Start(object obj, EventArgs e)
    {
       var engine = ViewEngines.Engines.OfType<RazorViewEngine>().Single();
       var newViewLocations = new string[] { 
           "~/SomeOtherFolder/{1}/{0}.cshtml", 
           "~/GlobalFolder/{0}.cshtml"      
       };
       engine.ViewLocationFormats = newViewLocations;
       engine.PartialViewLocationFormats = newViewLocations;
    }
    

    IIRC, {1} would correspond to controller and {0} to view name, you can look at existing properties to make sure.

    If you want to keep existing search locations you need to copy them into your new array.

    0 讨论(0)
提交回复
热议问题