BinaryWrite exception “OutputStream is not available when a custom TextWriter is used” in MVC 2 ASP.NET 4

前端 未结 3 1347
暖寄归人
暖寄归人 2021-01-01 18:16

I have a view rendering a stream using the response BinaryWrite method. This all worked fine under ASP.NET 4 using the Beta 2 but throws this exception in the RC release:

3条回答
  •  渐次进展
    2021-01-01 18:38

    I did Levi's answer. It's actually super easy. My code writes an image to the response, which is previously gotten from the file system after various checks.

    public class BookImageResult : ActionResult
    {
        private readonly GraphicReport graphicReport;
    
        public BookImageResult(GraphicReport graphicReport)
        {
            this.graphicReport = graphicReport;
        }
    
        public override void ExecuteResult(ControllerContext context)
        {
            var response = context.RequestContext.HttpContext.Response;
            response.Clear();
            response.ContentType = graphicReport.ContentType;
            response.BinaryWrite(graphicReport.Image);
            response.End();
        }
    }
    

    The line at the end of the controller just looks like this:

    return new BookImageResult(graphicReport);
    

    Someone mark Levi's response as the answer!

提交回复
热议问题