Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom RazorViewEngine gives page error

I am trying to implement a custom view engine with Razor. The goal is if the view is in a sub folder to use that view instead.

I have my view engine derived from the RazorViewEngine

 public class RazorViewFactory : RazorViewEngine
{
public RazorViewFactory()
{
    string TenantID = ConfigurationManager.AppSettings["TenantID"];

    if (TenantID != null)
    {
        MasterLocationFormats = new[] { 
            "~/Views/Shared/{0}.cshtml" 
        };

        ViewLocationFormats = new[]{
            "~/Tenant/" + TenantID + "/Views/{1}/{0}.cshtml",
            "~/Tenant/" + TenantID + "/Views/Shared/{0}.cshtml",
            "~/Views/{1}/{0}.cshtml",
            "~/Views/Shared/{0}.cshtml"
        };

        PartialViewLocationFormats = new[] { 
            "~/Tenant/" + TenantID + "/Views/{1}/{0}.cshtml", 
            "~/Tenant/" + TenantID + "/Views/Shared/{0}.cshtml" 
        };
    }
}

}

and in my Global.asax

 protected void Application_Start()
        {
            ...
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new RazorViewFactory()); 
        }

Everything works except when I load my Tenant sub view Home page, I get the following error.

The view at '~/Tenant/TenantB/Views/Home/Index.cshtml' 
must derive from WebViewPage, or WebViewPage<TModel>.

If I load the base home page it works fine with the Razor engine.

like image 698
Kenoyer130 Avatar asked Mar 14 '11 20:03

Kenoyer130


1 Answers

You need to copy the web.config file from your Views folder into your Tenant folder (or make sure it has the same config sections as described here: Razor HtmlHelper Extensions (or other namespaces for views) Not Found)

like image 181
marcind Avatar answered Sep 19 '22 23:09

marcind