渲染从WCF REST服务方法的MVC3行动为字符串 [英] Render an MVC3 action to a string from a WCF REST service method

查看:131
本文介绍了渲染从WCF REST服务方法的MVC3行动为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WCF REST服务,需要一些参数和发送电子邮件。对于电子邮件模板是一个MVC3动作。基本上我想要呈现的行动为字符串。

I have a WCF REST service that takes some parameters and sends an email. The template for the email is an MVC3 action. Essentially I want to render that action to a string.

如果它是一个ASP.NET的WebForm,我可以简单地使用使用Server.Execute(路径,StringWriter的,假的)。然而,当我插上的路径,我的行为,我得到错误执行子请求

If it were an ASP.NET WebForm, I could simply use Server.Execute(path, stringWriter, false). However when I plug in the path to my action, I get Error executing child request.

我从我的服务完全访问的HttpContext( AspNetCompatibilityRequirementsMode.Allowed )。

I have full access to HttpContext from my service (AspNetCompatibilityRequirementsMode.Allowed).

我知道有其他的答案在那里为从控制器的范围内渲染动作字符串。我如何做到这一点的时候,我那个世界之外,但仍然在同一服务器上(和,对于这个问题,在相同的应用程序)?

I know there are other answers out there for rendering actions to strings from within the context of a controller. How do I do this when I'm outside that world, but still on the same server (and, for that matter, in the same app)?

推荐答案

我拼凑一个答案基于几个不同的谷歌搜索。它的工作原理,但我不是100%肯定它是瘦,因为它可以。我会贴code为别人去尝试。

I cobbled together an answer based on several different google searches. It works, but I'm not 100% sure it's as lean as it could be. I'll paste the code for others to try.

string GetEmailText(TemplateParameters parameters) {
    // Get the HttpContext
    HttpContextBase httpContextBase = 
        new HttpContextWrapper(HttpContext.Current);
    // Build the route data
    var routeData = new RouteData();
    routeData.Values.Add("controller", "EmailTemplate");
    routeData.Values.Add("action", "Create");

    // Create the controller context
    var controllerContext = new ControllerContext(
        new RequestContext(httpContextBase, routeData), 
        new EmailTemplateController());

    var body = ((EmailTemplateController)controllerContext.Controller)
               .Create(parameters).Capture(controllerContext);
    return body;
}

// Using code from here:
// http://blog.approache.com/2010/11/render-any-aspnet-mvc-actionresult-to.html
public class ResponseCapture : IDisposable
{
    private readonly HttpResponseBase response;
    private readonly TextWriter originalWriter;
    private StringWriter localWriter;
    public ResponseCapture(HttpResponseBase response)
    {
        this.response = response;
        originalWriter = response.Output;
        localWriter = new StringWriter();
        response.Output = localWriter;
    }
    public override string ToString()
    {
        localWriter.Flush();
        return localWriter.ToString();
    }
    public void Dispose()
    {
        if (localWriter != null)
        {
            localWriter.Dispose();
            localWriter = null;
            response.Output = originalWriter;
        }
    }
}
public static class ActionResultExtensions
{
    public static string Capture(this ActionResult result, ControllerContext controllerContext)
    {
        using (var it = new ResponseCapture(controllerContext.RequestContext.HttpContext.Response))
        {
            result.ExecuteResult(controllerContext);
            return it.ToString();
        }
    }
}

这篇关于渲染从WCF REST服务方法的MVC3行动为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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