Embedding Razor views in class library as resources

泄露秘密 提交于 2019-12-14 04:27:28

问题


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.


回答1:


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.




回答2:


Try install-package EmbeddedResourceVirtualPathProvider




回答3:


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.



来源:https://stackoverflow.com/questions/4616511/embedding-razor-views-in-class-library-as-resources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!