ConfigurableJsonResult.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Web.Script.Serialization;
  7. using System.Web.Configuration;
  8. using System.Configuration;
  9. namespace EMIS.Web.Controls
  10. {
  11. public class ConfigurableJsonResult : JsonResult
  12. {
  13. public override void ExecuteResult(ControllerContext context)
  14. {
  15. if (context == null)
  16. {
  17. throw new ArgumentNullException("context");
  18. }
  19. if (JsonRequestBehavior == JsonRequestBehavior.DenyGet &&
  20. String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
  21. {
  22. throw new InvalidOperationException("This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.");
  23. }
  24. HttpResponseBase response = context.HttpContext.Response;
  25. if (!String.IsNullOrEmpty(ContentType))
  26. {
  27. response.ContentType = ContentType;
  28. }
  29. else
  30. {
  31. response.ContentType = "application/json";
  32. }
  33. if (ContentEncoding != null)
  34. {
  35. response.ContentEncoding = ContentEncoding;
  36. }
  37. if (Data != null)
  38. {
  39. JavaScriptSerializer serializer = new JavaScriptSerializer();
  40. ScriptingJsonSerializationSection section = ConfigurationManager.GetSection("system.web.extensions/scripting/webServices/jsonSerialization") as ScriptingJsonSerializationSection;
  41. if (section != null)
  42. {
  43. serializer.MaxJsonLength = section.MaxJsonLength;
  44. serializer.RecursionLimit = section.RecursionLimit;
  45. }
  46. response.Write(serializer.Serialize(Data));
  47. }
  48. }
  49. }
  50. }