Filter JSON->JSONP for .NET 3.5


/ Published in: C#
Save to your folder(s)

The filter only needs to know which requests need to be returned as JSONP, doesn't care how. Modify "OnBeginRequest" accordinly.
If original .asmx doesn't return JSON, extend it like example shows.
JSONP only works for GET requests, so don't forget to change [ScriptMethod] if needed.


Copy this code and paste it in your HTML
  1. <system.web><httpModules>
  2. <add name="ContentTypeModule" type="Project.ContentTypeModule, Project" />
  3.  
  4. // For not-JSON asmx, create another method or extend the ASMX, like this:
  5. [WebMethod]
  6. [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
  7. public TO HelloWorld()
  8. {
  9. return base.HelloWorld();
  10. }
  11.  
  12.  
  13.  
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using System.Web;
  18. using System.IO;
  19. using System.Text;
  20. using System.Xml;
  21.  
  22. namespace Project
  23. {
  24. public class ContentTypeModule : IHttpModule
  25. {
  26. private const string JSON_CONTENT_TYPE = "application/json; charset=utf-8";
  27.  
  28. #region IHttpModule Members
  29. public void Dispose()
  30. {
  31. }
  32.  
  33. public void Init(HttpApplication app)
  34. {
  35. app.BeginRequest += OnBeginRequest;
  36. app.ReleaseRequestState += OnReleaseRequestState;
  37. }
  38. #endregion
  39.  
  40. public void OnBeginRequest(object sender, EventArgs e)
  41. {
  42. HttpApplication app = (HttpApplication)sender;
  43. HttpRequest resquest = app.Request;
  44. if (!resquest.Url.AbsolutePath.Contains("WebServiceJSONP.asmx"))
  45. return;
  46. else
  47. app.Context.Request.ContentType = JSON_CONTENT_TYPE;
  48. }
  49.  
  50. public void OnReleaseRequestState(object sender, EventArgs e)
  51. {
  52. HttpApplication app = (HttpApplication)sender;
  53. HttpResponse response = app.Response;
  54. if (app.Context.Request.ContentType != JSON_CONTENT_TYPE) return;
  55.  
  56. response.Filter = new JsonResponseFilter(response.Filter);
  57. }
  58. }
  59.  
  60. public class JsonResponseFilter : Stream
  61. {
  62. private readonly Stream _responseStream;
  63. private long _position;
  64.  
  65. public JsonResponseFilter(Stream responseStream)
  66. {
  67. _responseStream = responseStream;
  68. }
  69.  
  70. public override bool CanRead { get { return true; } }
  71.  
  72. public override bool CanSeek { get { return true; } }
  73.  
  74. public override bool CanWrite { get { return true; } }
  75.  
  76. public override long Length { get { return 0; } }
  77.  
  78. public override long Position { get { return _position; } set { _position = value; } }
  79.  
  80. public override void Write(byte[] buffer, int offset, int count)
  81. {
  82. string strBuffer = Encoding.UTF8.GetString(buffer, offset, count);
  83. strBuffer = AppendJsonpCallback(strBuffer, HttpContext.Current.Request);
  84. byte[] data = Encoding.UTF8.GetBytes(strBuffer);
  85. _responseStream.Write(data, 0, data.Length);
  86. }
  87.  
  88. private string AppendJsonpCallback(string strBuffer, HttpRequest request)
  89. {
  90. return request.Params["callback"] + "(" + strBuffer + ");";
  91. }
  92.  
  93. public override void Close()
  94. {
  95. _responseStream.Close();
  96. }
  97.  
  98. public override void Flush()
  99. {
  100. _responseStream.Flush();
  101. }
  102.  
  103. public override long Seek(long offset, SeekOrigin origin)
  104. {
  105. return _responseStream.Seek(offset, origin);
  106. }
  107.  
  108. public override void SetLength(long length)
  109. {
  110. _responseStream.SetLength(length);
  111. }
  112.  
  113. public override int Read(byte[] buffer, int offset, int count)
  114. {
  115. return _responseStream.Read(buffer, offset, count);
  116. }
  117. }
  118. }

URL: http://bloggingabout.net/blogs/adelkhalil/archive/2009/08/14/cross-domain-jsonp-with-jquery-call-step-by-step-guide.aspx

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.