12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using System;
- using System.IO;
- using System.Text;
- using System.Web;
- using System.Web.Mvc;
- using Newtonsoft.Json;
- namespace Bowin.Common.Mvc
- {
- public class JsonNetResult : JsonResult
- {
- public JsonNetResult()
- {
- SerializerSettings = new JsonSerializerSettings
- {
- ReferenceLoopHandling = ReferenceLoopHandling.Error,
- DateFormatHandling = DateFormatHandling.IsoDateFormat
- };
- }
- public JsonSerializerSettings SerializerSettings { get; set; }
- public Formatting Formatting { get; set; }
- public override void ExecuteResult(ControllerContext context)
- {
- if (context == null)
- throw new ArgumentNullException("context");
- HttpResponseBase response = context.HttpContext.Response;
- response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/json";
- if (ContentEncoding != null)
- response.ContentEncoding = ContentEncoding;
- if (Data != null)
- {
- JsonTextWriter writer = new JsonTextWriter(response.Output)
- {
- Formatting = Formatting
- };
- JsonSerializer serializer = JsonSerializer.Create(SerializerSettings);
- serializer.Serialize(writer, Data); writer.Flush();
- }
- //if (context == null)
- // throw new ArgumentNullException("context");
- //if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
- // throw new InvalidOperationException("JSON GET is not allowed");
- //HttpResponseBase response = context.HttpContext.Response;
- //response.ContentType = string.IsNullOrEmpty(this.ContentType) ? "application/json" : this.ContentType;
- //if (this.ContentEncoding != null)
- // response.ContentEncoding = this.ContentEncoding;
- //if (this.Data == null)
- // return;
- //var scriptSerializer = JsonSerializer.Create(this.Settings);
- //using (var sw = new StringWriter())
- //{
- // scriptSerializer.Serialize(sw, this.Data);
- // response.Write(sw.ToString().Trim('[', ']'));
- //}
- }
- }
- }
|