123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Web.Mvc;
- using System.Web;
- using System.Web.Script.Serialization;
- using System.IO;
- using System.Globalization;
- using Newtonsoft.Json;
- namespace Ant.Service.Common
- {
-
-
-
- public static class FormatJsonExtension
- {
-
-
-
-
-
- public static FormatJsonResult JsonFormat(this Controller c, object data)
- {
- FormatJsonResult result = new FormatJsonResult();
- result.NotLigerUIFriendlySerialize = true;
- result.Data = data;
- return result;
-
- }
-
-
-
-
-
-
-
-
- public static FormatJsonResult JsonFormat(this Controller c, object data, bool IsError, string message)
- {
- FormatJsonResult result = new FormatJsonResult();
- result.Data = data;
- result.Message = message;
- result.IsError = IsError;
- return result;
- }
-
-
-
-
-
-
-
- public static FormatJsonResult JsonFormat(this Controller c, object data,SysOperate op)
- {
-
- if (!data.IsNullOrEmpty())
- {
- return JsonFormatSuccess(c, data, op.ToMessage(true));
- }
- return JsonFormatError(c,op.ToMessage(false));
- }
-
-
-
-
-
-
-
-
- public static FormatJsonResult JsonFormat(this Controller c, object data,bool status, SysOperate op)
- {
- if (status)
- {
- return JsonFormatSuccess(c, data, op.ToMessage(true));
- }
- return JsonFormatError(c, op.ToMessage(false));
- }
-
-
-
-
-
-
-
- public static FormatJsonResult JsonFormatSuccess(this Controller c, object data,string message)
- {
- return JsonFormat(c, data, false, message);
- }
-
-
-
-
-
-
- public static FormatJsonResult JsonFormatError(this Controller c, string message)
- {
- return JsonFormat(c, null, true, message);
- }
- }
-
-
-
- public class FormatJsonResult : ActionResult
- {
- private bool iserror = false;
-
-
-
- public bool IsError
- {
- get { return iserror; }
- set { this.iserror = value; }
- }
-
-
-
- public string Message { get; set; }
-
-
-
- public Object Data { get; set; }
-
-
-
- public bool NotLigerUIFriendlySerialize { get; set; }
- public override void ExecuteResult(ControllerContext context)
- {
-
- if (context == null)
- {
- throw new ArgumentNullException("context");
- }
- HttpResponseBase response = context.HttpContext.Response;
- response.ContentType = "application/json";
- StringWriter sw = new StringWriter();
- JsonSerializer serializer = JsonSerializer.Create(
- new JsonSerializerSettings
- {
-
- ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
- NullValueHandling = NullValueHandling.Ignore
-
- }
- );
-
-
- using (JsonWriter jsonWriter = new JsonTextWriter(sw))
- {
- jsonWriter.Formatting = Formatting.Indented;
- if (!NotLigerUIFriendlySerialize)
- serializer.Serialize(jsonWriter, this);
- else
- serializer.Serialize(jsonWriter, Data);
- }
- response.Write(sw.ToString());
- }
- }
- }
|