2009-06-03 43 views
24

由于一些奇怪的原因,我想直接从控制器操作将HTML写入Response流。 (我理解MVC分离,但这是一个特殊情况。)从动作写入输出流

我可以直接写入HttpResponse流吗?在那种情况下,控制器动作应该返回哪个IView对象?我可以返回'空'吗?

回答

8

是的,你可以直接写入响应。完成后,您可以调用CompleteRequest(),并且不需要返回任何内容。

例如:

// GET: /Test/Edit/5 
public ActionResult Edit(int id) 
{ 

    Response.Write("hi"); 
    HttpContext.ApplicationInstance.CompleteRequest(); 

    return View();  // does not execute! 
} 
+1

您应该避免Response.End()http://stevesmithblog.com/blog/use-httpapplication-completerequest-instead-of-response-end/ – 2009-06-03 05:10:26

+1

然后更新以使用CompleteRequest()。 – womp 2009-06-03 06:09:05

5

写自己的操作结果。这里的矿井中的一个的示例:

public class RssResult : ActionResult 
{ 
    public RssFeed RssFeed { get; set; } 

    public RssResult(RssFeed feed) { 
     RssFeed = feed; 
    } 

    public override void ExecuteResult(ControllerContext context) { 
     context.HttpContext.Response.ContentType = "application/rss+xml"; 
     SyndicationResourceSaveSettings settings = new SyndicationResourceSaveSettings(); 
     settings.CharacterEncoding = new UTF8Encoding(false); 
     RssFeed.Save(context.HttpContext.Response.OutputStream, settings); 
    } 
} 
43

我用从FileResult派生的类来实现这一点使用正常MVC模式:

/// <summary> 
/// MVC action result that generates the file content using a delegate that writes the content directly to the output stream. 
/// </summary> 
public class FileGeneratingResult : FileResult 
{ 
    /// <summary> 
    /// The delegate that will generate the file content. 
    /// </summary> 
    private readonly Action<System.IO.Stream> content; 

    private readonly bool bufferOutput; 

    /// <summary> 
    /// Initializes a new instance of the <see cref="FileGeneratingResult" /> class. 
    /// </summary> 
    /// <param name="fileName">Name of the file.</param> 
    /// <param name="contentType">Type of the content.</param> 
    /// <param name="content">Delegate with Stream parameter. This is the stream to which content should be written.</param> 
    /// <param name="bufferOutput">use output buffering. Set to false for large files to prevent OutOfMemoryException.</param> 
    public FileGeneratingResult(string fileName, string contentType, Action<System.IO.Stream> content,bool bufferOutput=true) 
     : base(contentType) 
    { 
     if (content == null) 
      throw new ArgumentNullException("content"); 

     this.content = content; 
     this.bufferOutput = bufferOutput; 
     FileDownloadName = fileName; 
    } 

    /// <summary> 
    /// Writes the file to the response. 
    /// </summary> 
    /// <param name="response">The response object.</param> 
    protected override void WriteFile(System.Web.HttpResponseBase response) 
    { 
     response.Buffer = bufferOutput; 
     content(response.OutputStream); 
    } 
} 

控制器方法现在将是这样的:

public ActionResult Export(int id) 
{ 
    return new FileGeneratingResult(id + ".csv", "text/csv", 
     stream => this.GenerateExportFile(id, stream)); 
} 

public void GenerateExportFile(int id, Stream stream) 
{ 
    stream.Write(/**/); 
} 

请注意,如果缓冲关闭,

stream.Write(/**/); 

变得非常缓慢。解决方案是使用BufferedStream。在一种情况下,性能提高了约100倍。见

Unbuffered Output Very Slow

3

如果你不想得到你自己的结果类型,你可以简单地写Response.OutputStream并返回new EmptyResult()