Search code examples
.netasp.net-mvcvirtualpathprovider

Using custom VirtualPathProvider to load embedded views that inherit a custom WebViewPage


Similar to this question I am using a custom VirtualPathProvider to retrieve views that are embedded in dlls. If I don't put @inherits System.Web.Mvc.WebViewPage on my view I get this error:

The view at '~/Views/Home/Index.cshtml' must derive from ViewPage, ViewPage<TModel>, ViewUserControl, or ViewUserControl<TModel>.

I'm using a custom BaseViewPage that inherits from WebViewPage. Before I started using this custom VirtualPathProvider, setting the new base page in the web.config worked. Now, if I try to inherit my custom class on the page with @inherits My.BaseViewPage I get the same error as above.

For completeness' sake I'll add that the page does start to compile if I inherit from WebViewPage, but it throws an error because there is code expecting the view to have properties from BaseViewPage.


Solution

  • It turns out that the problem had to do with the view engine I was using. To get the VirtualPathProvider working I replaced the view engine with a custom one that added view locations I needed. I accidentally inherited from WebFormsViewEngine instead of RazorViewEngine which is why the error I kept getting was for ViewPage instead of WebViewPage. Now that that's all sorted out my custom base view page works just fine.