Json HTTP模块流问题 [英] Json HTTP Module stream issue

查看:108
本文介绍了Json HTTP模块流问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTTP模块,用于清理Web服务返回的JSON(请参阅

I have an HTTP Module that I use to clean up the JSON returned by my web service (see http://www.codeproject.com/KB/webservices/ASPNET_JSONP.aspx?msg=3400287#xx3400287xx for an example of this.) Basically it relates to calling cross-domain JSON web services from javascript.

有一个JsonHttpModule,它使用JsonResponseFilter Stream类来写出JSON,而重载的Write方法应该将回调函数的名称包装在JSON周围,否则JSON错误会被删除,因为它需要标签.但是,如果JSON确实很长,则会多次调用Stream类中的Write方法,从而导致回调函数错误地插入JSON中途.在Stream类中是否有一种方法可以将回调函数最后包装在流周围,或指定以1 Write方法而不是在块中写入所有JSON?

There is this JsonHttpModule which uses a JsonResponseFilter Stream class to write out the JSON and the overloaded Write method is supposed to wrap the name of the callback function around the JSON, otherwise the JSON errors out as needing a label. However, if the JSON is really long, the Write method in the Stream class is called multiple times, causing the callback function to incorrectly get inserted midway through the JSON. Is there a way in the Stream class to wrap the callback function around the stream at the end or to specify that it write all of the JSON in 1 Write method instead of in chunks??

在这里它在JsonHttpModule中调用JsonResponseFilter:

Here's where it calls the JsonResponseFilter in the JsonHttpModule:

public void OnReleaseRequestState(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;

            if (!_Apply(app.Context.Request)) return;

            // apply response filter to conform to JSONP
            app.Context.Response.Filter =
                new JsonResponseFilter(app.Context.Response.Filter, app.Context);
        }

这是JsonResponseFilter Stream类中的Write方法,该方法被多次调用:

Here's the Write method in the JsonResponseFilter Stream class that gets called multiple times:

public override void Write(byte[] buffer, int offset, int count)
        {
            var b1 = Encoding.UTF8.GetBytes(_context.Request.Params["callback"] + "(");
            _responseStream.Write(b1, 0, b1.Length);

            _responseStream.Write(buffer, offset, count);

            var b2 = Encoding.UTF8.GetBytes(");");
            _responseStream.Write(b2, 0, b2.Length);
        }

感谢您的帮助! 贾斯汀

Thanks for any help! Justin

推荐答案

它多次触发该方法的原因是因为它将缓冲内容,然后将其发送到输出流.这是显示如何创建ViewState移动器HttpModule的示例.您可以从实现中获得一些想法.向下滚动到底部并查看结果.

The reason it fires the method multiple times is because it will buffer the contents and then send it to the output stream. Here is an example which shows how to create ViewState mover HttpModule. You can get some idea from the implementation. Scroll down to the bottom and see the result.

http://www.highoncoding.com/Articles/464_Filtering_Responses_Using_ASP_NET_Res.

http://www.highoncoding.com/Articles/464_Filtering_Responses_Using_ASP_NET_Response_Filters.aspx

这篇关于Json HTTP模块流问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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