title: Write binary data as a response in an ASP.NET MVC web control
body: <p>I am trying to get a control (not a controller -- a subclass of <code>System.Web.UI.WebControls.WebControl</code>), that can be used 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</p>
  
  <p>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".</p>
  
  <p>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.</p>
  
  <p>To simplify it -- imagine I want to make a web control that renders to:</p>
  
  <pre><code> &lt;img src="pageThatControlIsOn?controlImage"&gt;
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>Response.Clear();
  Response.OutputStream.Write(thePngData); // this throws in MVC
  // set the content type, etc
  Response.End();
  </code></pre>
  
  <p>How am I supposed to do that in an ASP.NET MVC control?</p>