如何获得应答QUOT;流"从MVC3 /剃刀的动作? [英] How to get a response "stream" from an action in MVC3/Razor?
问题描述
我使用MVC3,.NET4,C#。
I am using MVC3, .NET4, C#.
我需要创建使用剃刀查看一些XHTML。我通过一个动作做到这一点。
I need to create some XHTML using a Razor View. I do this via an Action.
public ActionResult RenderDoc(int ReportId)
{
//A new document is created.
return View();
}
然后我需要从这个输出,并将其转换为Word文档。我使用的是第三方组件做到这一点,它需要一个流或文件为读入转换为DOC一个XHTML的源代码,如下所示:
I then need to take the output from this and convert it to a 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:
document.Open(MyXhtmlStream,FormatType.Html,XHTMLValidationType.Transitional);
我的问题:
什么是称之为RenderDoc行动并获得结果作为流送入MyXhtmlStream。
What would be a good way to call the "RenderDoc" Action and obtain the result as a stream to feed into "MyXhtmlStream".
非常感谢。
编辑:我有另一个想法!
1)使该行动创建一个字符串(XHTMLString)内查看。我看到这样做对SO的方法。
1) Render the View within the action to create a String(XHTMLString). I have seen a method to do this on SO.
2)创建一个MemoryStream并把这个字符串转换成它。
2) Create a MemoryStream and put this string into it.
Stream MyStream = New MemoryStream("XHTMLString and encoding method");
EDIT2:根据达林的答案
我需要进一步clasyify一点,我希望通过调整达林的code为我的目的来做到这一点。
I need to clasyify a little further, and I hope to do this via tweaking Darin's code for my purpose.
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);
}
}
以上是接近我所需要的。注意第三方WordDocument类型。所以仍然是如何得到了strXhtmlDocument变成了Response.OutputStream问题?
The above is closer to what I need. Note the 3rd Party WordDocument type. So there is still the issue of how I get the "strXhtmlDocument" into the "Response.OutputStream?
推荐答案
我只想写一个自定义的ActionResult来处理:
I would just write a custom ActionResult to handle that:
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.
,然后只需有我的控制器操作返回这个自定义操作的结果:
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);
}
现在控制器动作并不需要处理管道code了。该控制器的动作确实是一个典型的控制器动作应该做的事:
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:
- 查询模型
- 通过这个模型,一个ActionResult
在您的情况下,模型是这样的文件
类或不管它被调用。
In your case the model is this Document
class or whatever it is called.
这篇关于如何获得应答QUOT;流"从MVC3 /剃刀的动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!