문제

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

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top