Write binary data as a response in an ASP.NET MVC web control

Posted by Lou Franco on Stack Overflow See other posts from Stack Overflow or by Lou Franco
Published on 2010-04-28T19:11:56Z Indexed on 2010/04/28 19:17 UTC
Read the original article Hit count: 398

Filed under:
|

I am trying to get a control that I wrote for ASP.NET to work in an ASP.NET MVC environment. Normally, the control does the normal thing, and that works fine

Sometimes it needs to respond by clearing the response, writing an image to the response stream and changing the content type. When you do this, you get an exception "OutputStream is not available when a custom TextWriter is used".

If I were a page or controller, I see how I can create custom responses with binary data, but I can't see how to do this from inside a control's render functions.

To simplify it -- imagine I want to make a web control that renders to:

 <img src="pageThatControlIsOn?controlImage">

And I know how to look at incoming requests and recognize query strings that should be routed to the control. Now the control is supposed to respond with a generated image (content-type: image/png -- and the encoded image). In ASP.NET, we:

Response.Clear();
Response.OutputStream.Write(thePngData); // this throws in MVC
// set the content type, etc
Response.End();

How am I supposed to do that in an ASP.NET MVC control?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about asp.net-mvc