1 namespace System.Web.Mvc {
5 using System.Web.Mvc.Resources;
6 using System.Web.Script.Serialization;
8 public class JsonResult : ActionResult {
11 JsonRequestBehavior = JsonRequestBehavior.DenyGet;
14 public Encoding ContentEncoding {
19 public string ContentType {
29 public JsonRequestBehavior JsonRequestBehavior {
34 public override void ExecuteResult(ControllerContext context) {
35 if (context == null) {
36 throw new ArgumentNullException("context");
38 if (JsonRequestBehavior == JsonRequestBehavior.DenyGet &&
39 String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) {
40 throw new InvalidOperationException(MvcResources.JsonRequest_GetNotAllowed);
43 HttpResponseBase response = context.HttpContext.Response;
45 if (!String.IsNullOrEmpty(ContentType)) {
46 response.ContentType = ContentType;
49 response.ContentType = "application/json";
51 if (ContentEncoding != null) {
52 response.ContentEncoding = ContentEncoding;
55 JavaScriptSerializer serializer = new JavaScriptSerializer();
56 response.Write(serializer.Serialize(Data));