Merge pull request #569 from knocte/fix_cairo_profile_versions_problem
[mono.git] / mcs / class / System.Web.Mvc / System.Web.Mvc / JsonResult.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\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
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\r
15     using System.Text;\r
16     using System.Web;\r
17     using System.Web.Script.Serialization;\r
18 \r
19     public class JsonResult : ActionResult {\r
20 \r
21         public Encoding ContentEncoding {\r
22             get;\r
23             set;\r
24         }\r
25 \r
26         public string ContentType {\r
27             get;\r
28             set;\r
29         }\r
30 \r
31         public object Data {\r
32             get;\r
33             set;\r
34         }\r
35 \r
36         public override void ExecuteResult(ControllerContext context) {\r
37             if (context == null) {\r
38                 throw new ArgumentNullException("context");\r
39             }\r
40 \r
41             HttpResponseBase response = context.HttpContext.Response;\r
42 \r
43             if (!String.IsNullOrEmpty(ContentType)) {\r
44                 response.ContentType = ContentType;\r
45             }\r
46             else {\r
47                 response.ContentType = "application/json";\r
48             }\r
49             if (ContentEncoding != null) {\r
50                 response.ContentEncoding = ContentEncoding;\r
51             }\r
52             if (Data != null) {\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
58             }\r
59         }\r
60     }\r
61 }\r