Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

c# - Dynamically Produce Razor Views at Runtime?

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

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.

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

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]?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Using custom VirtualPathProvider to load embedded resource Partial Views

Storing ASP.Net MVC Views in the Database

ASP.NET MVC load Razor view from database

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...