简体   繁体   English

在没有HttpContext或ControllerContext的情况下将ASP.NET MVC字符串渲染为View?

[英]Render ASP.NET MVC string to View without HttpContext or ControllerContext?

I need to render an ASP.NET MVC view to a string so as to be able to send it via an email (it is an order confirmation email defined in an .ascx file ). 我需要将ASP.NET MVC视图呈现为字符串,以便能够通过电子邮件发送它(它是.ascx文件中定义的订单确认电子邮件)。

I've successfully been able to render an ASP.NET MVC View to string using one of the methods in this question . 我已成功地使用此问题中的 一种方法将ASP.NET MVC视图呈现为字符串。

However now I need to be able to do it via a WCF service (that will be accessed via silverlight) and so I don't have a ControllerContext . 但是现在我需要能够通过WCF服务(可以通过silverlight访问)来实现它,所以我没有ControllerContext This WCF service is contained within the same project as my MVC project so has access to all my models etc. 此WCF服务包含在与我的MVC项目相同的项目中,因此可以访问我的所有模型等。

I've looked at several questions on Stackoverflow about this issue, but they all seem to need a controller context. 我在Stackoverflow上看了几个关于这个问题的问题,但它们似乎都需要一个控制器上下文。 I thought there was something in mvccontrib but it doesn't seem to be there anymore . 我认为mvccontrib中有一些东西,但它似乎不再存在了

The closest I've found is the accepted answer to the aforementioned question , but it unfortunately breaks with RenderPartial within the view you're rendering. 我发现的最接近的答案是上述问题可接受答案 ,但不幸的是,在你正在渲染的视图中,RenderPartial会中断。

I'm hoping maybe some of the behind the scenes work for ASP.NET MVC 2 related to RenderAction may help make this possible now? 我希望也许有些与RenderAction相关的ASP.NET MVC 2的幕后工作可能有助于实现这一目标吗?

为什么不创建一个ControllerContext,即使假的

The way that the web forms view engine is integrated into MVC requires the controller context as it actually bubbles the templating / rendering up to the ASP.NET Page class which writes the template content directly to the response stream. Web表单视图引擎集成到MVC中的方式需要控制器上下文,因为它实际上将模板化/呈现到ASP.NET页面类,它将模板内容直接写入响应流。

I suggest you take a look at the spark view engine (which will render WFVE templates without change) and use that to generate the templated emails from within the WCF service. 我建议你看一下spark视图引擎(它会在不改变的情况下呈现WFVE模板)并使用它来从WCF服务中生成模板化的电子邮件。 There are examples of this in the Spark download. Spark下载中有这方面的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM