PageRenderTime 3508ms CodeModel.GetById 21ms RepoModel.GetById 2ms app.codeStats 0ms

/Trunk/Sources/System.Web.Mvc/SystemWebMvc/Mvc/JsonResult.cs

#
C# | 60 lines | 51 code | 9 blank | 0 comment | 10 complexity | 40f6270e744f14195539a7e4ee49f088 MD5 | raw file
Possible License(s): LGPL-2.1
  1. namespace System.Web.Mvc {
  2. using System;
  3. using System.Text;
  4. using System.Web;
  5. using System.Web.Mvc.Resources;
  6. using System.Web.Script.Serialization;
  7. public class JsonResult : ActionResult {
  8. public JsonResult() {
  9. JsonRequestBehavior = JsonRequestBehavior.DenyGet;
  10. }
  11. public Encoding ContentEncoding {
  12. get;
  13. set;
  14. }
  15. public string ContentType {
  16. get;
  17. set;
  18. }
  19. public object Data {
  20. get;
  21. set;
  22. }
  23. public JsonRequestBehavior JsonRequestBehavior {
  24. get;
  25. set;
  26. }
  27. public override void ExecuteResult(ControllerContext context) {
  28. if (context == null) {
  29. throw new ArgumentNullException("context");
  30. }
  31. if (JsonRequestBehavior == JsonRequestBehavior.DenyGet &&
  32. String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) {
  33. throw new InvalidOperationException(MvcResources.JsonRequest_GetNotAllowed);
  34. }
  35. HttpResponseBase response = context.HttpContext.Response;
  36. if (!String.IsNullOrEmpty(ContentType)) {
  37. response.ContentType = ContentType;
  38. }
  39. else {
  40. response.ContentType = "application/json";
  41. }
  42. if (ContentEncoding != null) {
  43. response.ContentEncoding = ContentEncoding;
  44. }
  45. if (Data != null) {
  46. JavaScriptSerializer serializer = new JavaScriptSerializer();
  47. response.Write(serializer.Serialize(Data));
  48. }
  49. }
  50. }
  51. }