Change the Views location

前端 未结 3 707
粉色の甜心
粉色の甜心 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:47

    You'll need to create a custom view engine and use that instead. Fortunately you can just inherit from the default one and change the locations on the constructor. Here's a guide to creating your own view engine: http://www.singingeels.com/Articles/Creating_a_Custom_View_Engine_in_ASPNET_MVC.aspx

    From the article:

    protected void Application_Start()
    {
        //... other things up here.
    
        // I want to REMOVE the ASP.NET ViewEngine...
        ViewEngines.Engines.Clear();
    
        // and then add my own :)
        ViewEngines.Engines.Add(new HoTMeaTViewEngine());
    }
    
    public class HoTMeaTViewEngine : VirtualPathProviderViewEngine
    {
        public HoTMeaTViewEngine()
        {
            // This is where we tell MVC where to look for our files. This says
            // to look for a file at "Views/Controller/Action.html"
            base.ViewLocationFormats = new string[] { "~/Views/{1}/{0}.html" };
    
            base.PartialViewLocationFormats = base.ViewLocationFormats;
        }
    }
    

提交回复
热议问题