Mike Koder
Mike Koder

Reputation: 1928

Embedding Razor views in class library as resources

I'm using VirtualPathProvider to provide themed views.

In action method I want to return a view by path

return View("~/Themes/SomeTheme.dll/Views/Content/Item.cshtml");

In Visual Studio Item.cshtml has a build action "Embedded Resource".
VirtualPathProvider finds that .cshtml file, but I get an error

The view at '~/Themes/SomeTheme.dll/Views/Content/Item.cshtml' must derive from WebViewPage, or WebViewPage<TModel>.

I guess I have to compile that view and I've followed these instructions.

Now the VirtualPathProvider can't find the Item.cshtml because it's not an embedded resource anymore.

How do I solve this problem? I can't add reference to that SomeTheme.dll because activated theme is loaded dynamically at application start.

I've done this before with aspx files and those didn't need to be compiled. They worked out of the box as embedded resources.

Upvotes: 3

Views: 5753

Answers (3)

mcintyre321
mcintyre321

Reputation: 13306

Try install-package EmbeddedResourceVirtualPathProvider

Upvotes: 1

sky-dev
sky-dev

Reputation: 6258

Try this solution to handle intellisense in your View Class Library. You may need to add the following line to that web.config.

<add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />        

Also, try adding @Inherits System.Web.Mvc.WebViewPage statements to each of your embedded views (or substitute the base clase you use) to handle this at runtime.

Upvotes: 0

Tom Clarkson
Tom Clarkson

Reputation: 16174

I ran into a similar issue with embedded aspx views. The compilation that happens when the page is viewed relies on some settings in the web.config of the views folder. The embedded resources aren't in the views folder, so don't use those settings and fail to compile.

Upvotes: 2

Related Questions