Writing Binary Data as Response in ASP.NET MVC Web Control

I am trying to get a control (not a controller - a subclass System.Web.UI.WebControls.WebControl

) that can be used, which I wrote for ASP.NET to work in an ASP.NET MVC environment. Typically the controls do the usual thing and it works great

Sometimes it must 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 using a custom TextWriter."

If I was a page or controller, I can see how I can create custom responses with binary data, but I cannot see how to do this from within the control's render functions.

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

 <img src="pageThatControlIsOn?controlImage">

      

And I know how to look at incoming requests and recognize the request strings that should be redirected to the control. The control should now respond with a generated image (content-type: image / png - and an 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 this in an ASP.NET MVC control?

+2


a source to share


1 answer


You can create a custom ActionResult

. Here is a link to a blog post for creating a custom class ImageResult

that subclasses ActionResult

. http://blog.maartenballiauw.be/post/2008/05/13/ASPNET-MVC-custom-ActionResult.aspx



0


a source







All Articles