Posted By

FMalk on 01/06/11


Tagged

ajax Net json JSONP asmx


Versions (?)

Filter JSON->JSONP for .NET 3.5


 / Published in: C#
 

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

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.

  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. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Paulson357 on August 16, 2018

The json filter converts a JavaScript object into a JSON string. This filter can be useful when debugging your applications. The JavaScript object can be any kind of JavaScript object. tellthebell

Posted By: macmomi on February 11, 2019

he json channel changes over a JavaScript object into a JSON string. <a href="> Tellthebell channel can be valuable while investigating your applications. The JavaScript article can be any sort of JavaScript object.

Posted By: macmomi on February 11, 2019

Json channel changes over a JavaScript object into a JSON string. This channel can be valuable while investigating your applications. The JavaScript article can be any sort of JavaScript object <a href="> Tellthebell

Posted By: sirnocares on March 11, 2019

tellthebell Taco bell survey offers you a chance to give feedback and recommendations about your visit at taco bell and win prize money!

Posted By: steveharis22 on March 17, 2019

This is really nice guide I liked it. Taco Bell is providing a chance for its customers to win $500 in cash if they complete the Tellthebell – Taco Bell Survey. Taco Bell is a bit different from other fast-food chains. It has distinguished itself from its competitors like McDonald’s, Burger King, and lots of others.

Posted By: justin001 on June 6, 2019

Really I enjoy your site with effective and useful information. It is included very nice post with a lot of our resources.thanks for share. i enjoy this post. Percy And Founders Drink Menu

Posted By: williamruss001 on June 13, 2019

Such a great article! My newest hobby is participating in online surveys and turns out that this kind of hobby can bring you benefits such as cash and free product samples. Tell Tims Survey

You need to login to post a comment.