5
votes

I have an MVC project that requires there to be 2 different View folders. One is at ~/Views/ and one at ~/Framework/Views/. This is done by creating a custom view engine based on the razor view engine like this:

public class MyViewEngine : RazorViewEngine
{
    private static string[] AdditionalViewLocations = new[]{
        "~/Framework/Views/{1}/{0}.cshtml",
        "~/Framework/Views/{1}/{0}.vbhtml",
        "~/Framework/Views/Shared/{0}.cshtml",
        "~/Framework/Views/Shared/{0}.vbhtml"
    };

    public MyViewEngine()            
    {
        base.PartialViewLocationFormats = base.PartialViewLocationFormats.Union(AdditionalViewLocations).ToArray();
        base.ViewLocationFormats = base.ViewLocationFormats.Union(AdditionalViewLocations).ToArray();
        base.MasterLocationFormats = base.MasterLocationFormats.Union(AdditionalViewLocations).ToArray();
    }
}

The problem is that I want to use a different _ViewStart.cshtml file in each of the 2 Views folder (i.e. ~/Views/_ViewStart.cshtml for views found in the ~/Views/ folder and ~/Framework/Views/_ViewStart.cshtml for views found in the ~/Framework/Views/ Folder), however the View Engine just uses the first one it finds which is the original one in ~/Views/.

Is this possible to do?

Thank you

2
Why not use Areas instead of messing with view engine?Nikola Radosavljević
This is definitely possible - in fact I just whipped it up on my machine using the view engine you supplied, just copied and pasted. I am not seeing the same behavior as you. I have two _ViewStart files, one at ~/Framework/Views/_ViewStart.cshtml, and one at ~/Views/_ViewStart.cshtml. When I run a view within ~/Framework/Views/, it uses the Framework _ViewStart. When I run a view within ~/Views/, it uses the _ViewStart there. Double checking the code in RazorViewEngine using DotPeek also confirms that this is how it should behave. Are you sure you aren't missing something?user22467
@NickAceves: Thanks you're right. My file was saved in the wrong location and a duplicate of the wrong file was saved in the right location! What a dooch! Thanks again. (put it in an answer and I'll mark it)hofnarwillie

2 Answers

7
votes

This is definitely possible, I think you just missed something.

I have tested this myself using the view engine you supplied (copied and pasted verbatim). I am not seeing the same behavior as you. I have two _ViewStart.cshtml files, one at ~/Framework/Views/_ViewStart.cshtml, and one at ~/Views/_ViewStart.cshtml.

When I run a view within ~/Framework/Views/, it uses the _ViewStart.cshtml in the Framework folder. When I run a view within ~/Views/, it uses the _ViewStart.cshtml in the Views folder.

Double checking the code in RazorViewEngine using DotPeek also confirms that this is exactly how it should behave. The view engine starts checking in for a file named _ViewStart.cshtml within the same folder as the view being rendered, and then walks up the directory tree until it gets to the root of the application.

3
votes

The selection of _ViewStart is hierarchical, but you've added ~/Framework/Views parallel to ~/Views. I don't think Razor is set up to actually do what you want (i.e. two completely parallel view locations). If you were to put Framework into the main Views folder, your _ViewStarts would load properly, though.