using System; using System.Text; using System.Web; using System.Web.Mvc; using ApacKernel; using Newtonsoft.Json; namespace Now.Web.JsonNet { /// /// with Json.Net serialization /// public class JsonNetResult : JsonResult { /// /// Create action result /// public JsonNetResult() { Settings = new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Error, Formatting = Formatting.None }; } internal JsonNetResult(JsonSerializerSettings settings) { Settings = settings; } /// /// Json.Net Serialization settings /// public JsonSerializerSettings Settings { get; private set; } /// public override void ExecuteResult(ControllerContext context) { Ensure.Argument.NotNull(context, "context"); if (JsonRequestBehavior == JsonRequestBehavior.DenyGet && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) throw new InvalidOperationException("JSON GET is not allowed"); var response = context.HttpContext.Response; response.ContentType = string.IsNullOrEmpty(ContentType) ? "application/json" : ContentType; if (ContentEncoding != null) response.ContentEncoding = ContentEncoding; if (Data == null) return; var formatting = HttpContext.Current != null && HttpContext.Current.IsDebuggingEnabled ? Formatting.Indented : Settings.Formatting; response.Write(JsonConvert.SerializeObject(Data, formatting, Settings)); } } /// /// extentsions. /// public static class JsonNetResultExtensions { /// /// Creates a object that serializes the specified object to JavaScript Object Notation (JSON). /// /// /// /// The JSON result object that serializes the specified object to JSON format. The result object that is prepared by this method is written to the response by the ASP.NET MVC framework when the object is executed. /// /// The current controller. /// The JavaScript object graph to serialize. /// The Json.Net serialization settings public static JsonResult JsonNet(this Controller controller, object data, JsonSerializerSettings settings = null) { return JsonNet(controller, data, (string)null, (Encoding)null, JsonRequestBehavior.DenyGet, settings); } /// /// Creates a object that serializes the specified object to JavaScript Object Notation (JSON) format. /// /// /// /// The JSON result object that serializes the specified object to JSON format. /// /// The current controller. /// The JavaScript object graph to serialize.The content type (MIME type). /// The Json.Net serialization settings public static JsonResult JsonNet(this Controller controller, object data, string contentType, JsonSerializerSettings settings = null) { return JsonNet(controller, data, contentType, (Encoding)null, JsonRequestBehavior.DenyGet, settings); } /// /// Creates a object that serializes the specified object to JavaScript Object Notation (JSON) format. /// /// /// /// The JSON result object that serializes the specified object to JSON format. /// /// The current controller. /// The JavaScript object graph to serialize.The content type (MIME type).The content encoding. /// The Json.Net serialization settings public static JsonResult JsonNet(this Controller controller, object data, string contentType, Encoding contentEncoding, JsonSerializerSettings settings = null) { return JsonNet(controller, data, contentType, contentEncoding, JsonRequestBehavior.DenyGet, settings); } /// /// Creates a object that serializes the specified object to JavaScript Object Notation (JSON) format using the specified JSON request behavior. /// /// /// /// The result object that serializes the specified object to JSON format. /// /// The current controller. /// The JavaScript object graph to serialize.The JSON request behavior. /// The Json.Net serialization settings public static JsonResult JsonNet(this Controller controller, object data, JsonRequestBehavior behavior, JsonSerializerSettings settings = null) { return JsonNet(controller, data, (string)null, (Encoding)null, behavior, settings); } /// /// Creates a object that serializes the specified object to JavaScript Object Notation (JSON) format using the specified content type and JSON request behavior. /// /// /// /// The result object that serializes the specified object to JSON format. /// /// The current controller. /// The JavaScript object graph to serialize.The content type (MIME type).The JSON request behavior /// The Json.Net serialization settings public static JsonResult JsonNet(this Controller controller, object data, string contentType, JsonRequestBehavior behavior, JsonSerializerSettings settings = null) { return JsonNet(controller, data, contentType, (Encoding)null, behavior, settings); } /// /// Creates a object that serializes the specified object to JavaScript Object Notation (JSON) format using the content type, content encoding, and the JSON request behavior. /// /// /// /// The result object that serializes the specified object to JSON format. /// /// The current controller. /// The JavaScript object graph to serialize.The content type (MIME type).The content encoding.The JSON request behavior /// The Json.Net serialization settings public static JsonResult JsonNet(this Controller controller, object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior, JsonSerializerSettings settings = null) { return settings != null ? new JsonNetResult(settings) { Data = data, ContentType = contentType, ContentEncoding = contentEncoding, JsonRequestBehavior = behavior } : new JsonNetResult { Data = data, ContentType = contentType, ContentEncoding = contentEncoding, JsonRequestBehavior = behavior }; } } }