简体   繁体   English

如何从MVC3 / Razor中的动作获得响应“流”?

[英]How to get a response “stream” from an action in MVC3/Razor?

I am using MVC3, .NET4, C#. 我正在使用MVC3,.NET4,C#。

I need to create some XHTML using a Razor View. 我需要使用Razor View创建一些XHTML。 I do this via an Action. 我通过动作做到这一点。

    public ActionResult RenderDoc(int ReportId)
    {
        //A new document is created.

        return View();
    }

I then need to take the output from this and convert it to a Word Doc. 然后我需要从中获取输出并将其转换为Word Doc。 I am using a 3rd party component to do this and it expects a "stream" or a "file" for the XHTML source that is read in for conversion to a DOC, like the following: 我正在使用第三方组件执行此操作,它期望读取XHTML源的“流”或“文件”以转换为DOC,如下所示:

document.Open(MyXhtmlStream,FormatType.Html,XHTMLValidationType.Transitional);

My Question: 我的问题:

What would be a good way to call the "RenderDoc" Action and obtain the result as a stream to feed into "MyXhtmlStream". 调用“RenderDoc”Action并将结果作为流提供给“MyXhtmlStream”的好方法是什么。

Many thanks. 非常感谢。

EDIT: I have had another idea !!! 编辑:我有另一个想法!

1) Render the View within the action to create a String(XHTMLString). 1)在动作中渲染视图以创建String(XHTMLString)。 I have seen a method to do this on SO. 我已经看到了在SO上执行此操作的方法。

2) Create a MemoryStream and put this string into it. 2)创建一个MemoryStream并将此字符串放入其中。

Stream MyStream = New MemoryStream("XHTMLString and encoding method");

EDIT2: Based on Darin's answer EDIT2:基于达林的回答

I need to clasyify a little further, and I hope to do this via tweaking Darin's code for my purpose. 我需要进一步理解,我希望通过为我的目的调整Darin的代码来做到这一点。

 public class XmlDocumentResult : ActionResult
 {
  private readonly string strXhtmlDocument;
  public XmlDocumentResult(string strXhtmlDocument)
  {
    this.strXhtmlDocument = strXhtmlDocument;
  }

  public override void ExecuteResult(ControllerContext context)
  {
    WordDocument myWordDocument = new WordDocument();
    var response = context.HttpContext.Response;
    response.ContentType = "text/xml";
    myWordDocument.Open(response.OutputStream, FormatType.Html, XHTMLValidationType.Transitional);
  }
 }

The above is closer to what I need. 以上更接近我的需要。 Note the 3rd Party WordDocument type. 请注意第三方WordDocument类型。 So there is still the issue of how I get the "strXhtmlDocument" into the "Response.OutputStream? 所以仍然存在如何将“strXhtmlDocument”放入“Response.OutputStream”的问题?

I would just write a custom ActionResult to handle that: 我只想写一个自定义ActionResult来处理:

public class XmlDocumentResult : ActionResult
{
    private readonly Document document;
    public XmlDocumentResult(Document document)
    {
        this.document = document;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        var response = context.HttpContext.Response;
        response.ContentType = "text/xml";
        document.Open(response.OutputStream, FormatType.Html, XHTMLValidationType.Transitional);
    }
}

You could of course adjust the response Content-Type if necessary and also append a Content-Disposition header if you want. 当然,您可以根据需要调整响应Content-Type如果需要,还可以附加Content-Disposition标头。

And then simply have my controller action return this custom action result: 然后让我的控制器操作返回此自定义操作结果:

public ActionResult RenderDoc(int reportId)
{
    Document document = repository.GetDocument(reportId);
    return new XmlDocumentResult(document);
}

Now the controller action doesn't need to handle plumbing code anymore. 现在控制器操作不再需要处理管道代码了。 The controller action does what a typical controller action is supposed to do: 控制器操作执行典型的控制器操作应该执行的操作:

  1. Query the Model 查询模型
  2. Pass this model to an ActionResult 将此模型传递给ActionResult

In your case the model is this Document class or whatever it is called. 在您的情况下,模型是此Document类或其所谓的任何类。

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

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