Question

I created a custom Razor View engine

Right now when I do:

public class ConfigViewEngine : RazorViewEngine
{
    public ConfigViewEngine()
    {
        var existingViewLocationFormats = new List<string>();
        var partialViewLocationFormats = new List<string>();

        //Folder Structure: Views\Desktop\Home and Views\Mobile\Home
        existingViewLocationFormats.Insert(0,
                                         "~/Views/" + ConfigurationManager.AppSettings["configName"] +
                                         "/Shared/{0}.cshtml");
        partialViewLocationFormats.Insert(0,
                                        "~/Views/" + ConfigurationManager.AppSettings["configName"] +
                                        "/Shared/{0}.cshtml");
        existingViewLocationFormats.Insert(0,
                                         "~/Views/" + ConfigurationManager.AppSettings["configName"] +
                                         "/{1}/{0}.cshtml");
        partialViewLocationFormats.Insert(0,
                                        "~/Views/" + ConfigurationManager.AppSettings["configName"] +
                                        "/{1}/{0}.cshtml");

        ViewLocationFormats = existingViewLocationFormats.ToArray();
        PartialViewLocationFormats = partialViewLocationFormats.ToArray();
    }
}

My _ViewStart.cshtml looks like

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new ConfigViewEngine());
Layout = "~/Views/" + ConfigurationManager.AppSettings["configName"] + "/Shared/_Layout.cshtml";

I get the following error when I go to /Home/Dashboard

The view 'Dashboard' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Dashboard.aspx
~/Views/Home/Dashboard.ascx
~/Views/Shared/Dashboard.aspx
~/Views/Shared/Dashboard.ascx
~/Views/Home/Dashboard.cshtml
~/Views/Home/Dashboard.vbhtml
~/Views/Shared/Dashboard.cshtml
~/Views/Shared/Dashboard.vbhtml

If I place an empty Dashboard.cshtml file in /Views/Home/, it will use that file and then when I refresh, it will serve the correct file from /Views/configName/Home/Dashboard

Anybody know why it's not initializing correctly?

Was it helpful?

Solution

Figured it out. I wasn't supposed to put that code in _ViewStart.cshtml, I was supposed to put it in Global.asax.cs

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top