将 MVC3 操作从 WCF REST 服务方法呈现为字符串

发布于 2024-11-30 16:58:06 字数 372 浏览 0 评论 0原文

我有一个 WCF REST 服务,它接受一些参数并发送电子邮件。电子邮件的模板是 MVC3 操作。本质上我想将该操作呈现为字符串。

如果它是 ASP.NET WebForm,我可以简单地使用 Server.Execute(path, stringWriter, false)。但是,当我插入操作的路径时,我收到执行子请求时出错

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

我知道还有其他答案可以从控制器的上下文中将操作呈现给字符串。当我不在那个世界但仍在同一服务器上(并且就此而言,在同一个应用程序中)时,我该如何执行此操作?

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.

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捶死心动 2024-12-07 16:58:06

我根据几个不同的谷歌搜索拼凑了一个答案。它有效,但我不能 100% 确定它是否尽可能精简。我将代码粘贴给其他人尝试。

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();
        }
    }
}

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();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文