如何呈现的Razor视图在ASP.NET MVC 3串? [英] How to render a Razor View to a string in ASP.NET MVC 3?

查看:137
本文介绍了如何呈现的Razor视图在ASP.NET MVC 3串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找的网站很多,但所有我能找到的是如何使局部控制的.ascx ,或者依赖于一个控制器上下文的例子。

I've been searching the site a lot, but all I could find were examples on how to render partial controls .ascx, or depended on a controller context.

我想,让我的方法,以只提供给视图的相对路径和模式,使该视图与模型转换为字符串:

I want a method that enables me to provide just the relative path to the view, and a model, and render that view with that model into a string:

string result = Utility.RenderViewToString("~/Views/My/Profile.cshtml", model);

我能找到的所有的例子都是无论是旧版本的MVC,或根本没有做什么,我需要做。

All the examples I could find were either for older versions of MVC, or simply didn't do what I need to do here.

感谢您

推荐答案

可以做到这一点与 razorengine

string template = "Hello @Model.Name! Welcome to Razor!";
string result = Razor.Parse(template, new { Name = "World" });

和它不依赖于控制器上下文 - 而是因为,你是不是能够使用HTML佣工(这依赖于HTTP上下文)。但是,它是完美的使用剃刀作为字符串的模板引擎。

And it does not rely on the controller context - but because of that you are not able to use the Html helpers (which rely on the http context). But it is perfect to use razor as a template engine for strings.

这篇关于如何呈现的Razor视图在ASP.NET MVC 3串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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