写二进制数据作为一个ASP.NET MVC Web控件响应 [英] Write binary data as a response in an ASP.NET MVC web control

查看:90
本文介绍了写二进制数据作为一个ASP.NET MVC Web控件响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图得到控制(不是控制器 - System.Web.UI.WebControls.WebControl 的子类),可以使用我写为ASP.NET在ASP.NET MVC环境中工作。通常情况下,控制不正常的东西,那正常工作

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

有时需要通过清除响应,写入响应流的图像和改变内容类型作出响应。当你这样做,你会得到一个异常的OutputStream时不可用自定义的TextWriter使用。

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.

要简化它 - 想象我想要呈现给Web控件:

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

 <img src="pageThatControlIsOn?controlImage">

我知道如何看待传入的请求,并认识到应该被路由到控制查询字符串。现在控制应该与所生成的图像(内容类型:image / PNG - 和EN codeD映像)响应。在ASP.NET中,我们:

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();

我怎么做,在一个ASP.NET MVC的控制?

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

推荐答案

您可以创建一个自定义的的ActionResult 。这里是一个博客帖子的链接创建自定义 ImageResult 类的子类的ActionResult 。的http://blog.maartenballiauw.be/post/2008/05/13/ASPNET-MVC-custom-ActionResult.aspx

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

这篇关于写二进制数据作为一个ASP.NET MVC Web控件响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆