有没有一种方便的方法可以从字符串返回视图,而不必从磁盘上的文件返回视图?
我已经实现了一个自定义 VirtualPathProvider
来处理从数据库检索视图,但我并不总是希望将视图存储在数据库中。
更新 2-15-2011
我偶然发现了一个非常好的开源组件,它简化了在代码中编译 Razor 视图的过程。我已经用这个组件替换了大部分虚拟路径提供程序代码,并且它运行得非常好。我将其推荐给任何尝试从数据库或其他地方编译视图且不需要虚拟路径提供程序附加功能的人。该组件允许您直接在控制器/应用程序/任何内容(不需要 Web 上下文和控制器上下文)中编译视图,而无需跳过 VPP 环。
Is there a convenient way to return a view from a string instead of having to come from a file on disk?
I've implemented a custom VirtualPathProvider
that handles retrieving views from a database, but I don't always want the view to be stored in the database.
Update 2-15-2011
I stumbled across a very nice open source component that simplifies the process of compiling Razor views in code.I've replaced much of the Virtual Path Provider code with this component, and it's working incredibly well. I recommend it to anyone that's trying to compile views from a database or elsewhere who doesn't need the additional capabilities of a virtual path provider. This component lets you compile the view directly within your controller/app/whatever (web context and controller context not required) without having to jump through the VPP hoops.
发布评论
评论(1)
您可以通过创建一个
RazorTemplateEngine
来自行运行该视图,该引擎读取您的源代码并编译为WebViewPage
。WebViewPage
然后,您可以通过写入支持新的
@model
关键字,您需要重写 RazorEngineHost 中的方法才能使用 MVC 的自定义生成器:You can run the view yourself by creating a
RazorTemplateEngine
which reads your source and compiles into aWebViewPage
.You can then run the
WebViewPage
by writingTo support the new
@model
keyword, you'll need to override methods in your RazorEngineHost to use MVC's custom generators: