将Razor视图作为资源嵌入到类库中 [英] Embedding Razor views in class library as resources

查看:185
本文介绍了将Razor视图作为资源嵌入到类库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VirtualPathProvider提供主题视图.

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");

在Visual Studio中,Item.cshtml具有一个生成动作嵌入式资源".
VirtualPathProvider找到该.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.

现在,VirtualPathProvider找不到Item.cshtml,因为它不再是嵌入式资源.

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

如何解决此问题?我无法添加对SomeTheme.dll的引用,因为已激活的主题会在应用程序启动时动态加载.

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

我之前已经使用aspx文件完成了这些操作,而这些文件不需要进行编译.他们开箱即用地作为嵌入式资源.

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.

推荐答案

我在嵌入的aspx视图中遇到了类似的问题.查看页面时发生的编译依赖于views文件夹的web.config中的某些设置.嵌入式资源不在views文件夹中,因此请不要使用这些设置并且编译失败.

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.

这篇关于将Razor视图作为资源嵌入到类库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆