在运行时动态生成 Razor 视图? [英] Dynamically Produce Razor Views at Runtime?

查看:32
本文介绍了在运行时动态生成 Razor 视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑构建一个 Web 可配置的布局引擎,并想直接使用引擎盖下的 razor 引擎.

I am looking at building a web configurable layout engine, and would like to directly use the razor engine under the hood.

假设我有一些可通过类、xml 或数据库获得的信息 - 我想在运行时将其转换为本机 razor 代码(我的意思是暗示我定义内容和结构基于提供者模型).与其在每个请求中都动态地转换 xml 文件或其他东西,我想我还不如编写 Razor,让视图引擎处理所有事情.

Lets say I have some information available via classes, xml, or a database - I want to convert that into native razor code at runtime (by this I mean to imply my source of defining content and structure is based on a provider model). Rather than convert an xml file or something on the fly every request I figured I might as well just write Razor, and let that view engine take care of everything.

我可以将 razor 代码从非文件系统的来源直接插入引擎吗?

Can I plug razor code directly into the engine from a source that is not the file system?

如果我在运行时动态生成 .cshtml 视图和布局到文件中[并在控制器动作中动态设置这些视图],会有任何问题吗?

Will there be any gotchas if I dynamically generate .cshtml views and layouts into files at run-time [and dynamically set those views in the controller actions]?

推荐答案

是可以的.Razor 会比经典的 Webforms 视图引擎有更多的问题,但这里有一些资源:

It is possible to do. Razor will have some more gotcha moments than classic Webforms view engine but here are some resources:

http://rebuildall.umbraworks.net/2009/11/17/ASP_NET_MVC_and_virtual_views

使用自定义 VirtualPathProvider 加载嵌入的资源部分视图

在数据库中存储 ASP.Net MVC 视图

ASP.NET MVC 从数据库加载 Razor 视图

您将创建一个自定义 ViewEngine,用于确定在何处查找视图.视图可以存储在文件系统、数据库中或作为嵌入式资源.阅读我提供的链接,它应该会让你开始!

You'll be creating a custom ViewEngine that will determine where to look for the views. Views can be stored on the file system, in the database or as an embedded resources. Read up on the links I provided and it should get you going!

这篇关于在运行时动态生成 Razor 视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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