1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\r
5 * This software is subject to the Microsoft Public License (Ms-PL).
\r
6 * A copy of the license can be found in the license.htm file included
\r
7 * in this distribution.
\r
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc {
\r
17 using System.Web.Script.Serialization;
\r
19 public class JsonResult : ActionResult {
\r
21 public Encoding ContentEncoding {
\r
26 public string ContentType {
\r
31 public object Data {
\r
36 public override void ExecuteResult(ControllerContext context) {
\r
37 if (context == null) {
\r
38 throw new ArgumentNullException("context");
\r
41 HttpResponseBase response = context.HttpContext.Response;
\r
43 if (!String.IsNullOrEmpty(ContentType)) {
\r
44 response.ContentType = ContentType;
\r
47 response.ContentType = "application/json";
\r
49 if (ContentEncoding != null) {
\r
50 response.ContentEncoding = ContentEncoding;
\r
53 // The JavaScriptSerializer type was marked as obsolete prior to .NET Framework 3.5 SP1
\r
54 #pragma warning disable 0618
\r
55 JavaScriptSerializer serializer = new JavaScriptSerializer();
\r
56 response.Write(serializer.Serialize(Data));
\r
57 #pragma warning restore 0618
\r