2

I am trying to get a control (not a controller -- a subclass of System.Web.UI.WebControls.WebControl), 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

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?

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • possible answer: http://stackoverflow.com/questions/186062/can-an-asp-net-mvc-controller-return-an-image – Glennular Apr 28 '10 at 19:14
  • I am a control, not a controller. Also, the image is created on the fly (not a file). I think I can provide a byte array to the FileResult, but I don't see how a WebControl has access to the FileResult to do that. – Lou Franco Apr 28 '10 at 19:34
  • clarified my question -- I am writing a subclass of System.Web.UI.WebControls.WebControl, not a controller. I need to implement render to return an img tag and then the subsequent image when it's requested. – Lou Franco Apr 28 '10 at 19:42
  • You said ASP.NET MVC control. There's no such thing. – Trevor de Koekkoek Jul 27 '12 at 18:23

1 Answers1

0

You could create a custom ActionResult. Here is a link to a blog post for creating a custom ImageResult class that subclasses ActionResult. http://blog.maartenballiauw.be/post/2008/05/13/ASPNET-MVC-custom-ActionResult.aspx

mikefrey
  • 4,653
  • 3
  • 29
  • 40
  • Sorry, I tried this, but I still can't figure out how I get data from inside the render of a WebControl somewhere where a custom ActionResult could write it. I am trying to provide a control that could be reused by different MVC applications -- I am not writing controllers and views. I can also provide custom action results, but I can't see how to tie that together with a webcontrol – Lou Franco Apr 28 '10 at 20:09
  • This is the closest answer to what I think one should do. The other part is to write a route generator and your own controller. Have the user of the control use you route generator to add more routes that go to a controller you provide -- then your controller should make calls to your webcontrol (passing in a proper stream -- inside a custom ActionResult -- for it to write out to). – Lou Franco Apr 29 '10 at 14:23