Change the Views location

11,498

Solution 1

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

Solution 2

Check this place out. How to change default view location scheme in ASP.NET MVC?

                           base.ViewLocationFormats = new string[] { 
                "~/Views/{1}/{2}/{0}.aspx", 
                "~/Views/{1}/{2}/{0}.ascx", 
                "~/Views/Shared/{2}/{0}.aspx", 
                "~/Views/Shared/{2}/{0}.ascx" ,
                 "~/Views/{1}/{0}.aspx", 
                "~/Views/{1}/{0}.ascx", 
                "~/Views/Shared/{0}.aspx", 
                "~/Views/Shared/{0}.ascx" 

Even easier is this one Can I specify a custom location to “search for views” in ASP.NET MVC?

Share:
11,498
Vinay Kumar Chella
Author by

Vinay Kumar Chella

I am an SDE in an MNC

Updated on June 04, 2022

Comments

  • Vinay Kumar Chella
    Vinay Kumar Chella almost 2 years

    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 am getting following errors

    The view 'Index' or its master was not found. The following locations were searched:
    ~/Views/Search/Index.aspx
    ~/Views/Search/Index.ascx
    ~/Views/Shared/Index.aspx
    ~/Views/Shared/Index.ascx
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    

    My Views folder will be in ~/XYZ/ABC/Views instead of ~/Views. Please solve my problem. Will I get any problems If I change the default Views folder location. Do I need to change anything in HTML Helper classes because I don't know anything in MVC as this is my starting project i dont want to risk..Please help me out...