ylbtech-Unitity: cs-JsonHelper |
AjaxResult.cs FormatJsonExtension.cs
1.A,效果图 |
1.B,源代码 |
1.B.1,AjaxResult.cs View Code View Code
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web.Script.Serialization;namespace Core.JsonHelper{ ////// 前台Ajax请求的统一返回结果类 /// public class AjaxResult { private AjaxResult() { } private bool iserror = false; ////// 是否产生错误 /// public bool IsError { get { return iserror; } } ////// 错误信息,或者成功信息 /// public string Message { get; set; } ////// 成功可能时返回的数据 /// public object Data { get; set; } #region Error public static AjaxResult Error() { return new AjaxResult() { iserror = true }; } public static AjaxResult Error(string message) { return new AjaxResult() { iserror = true, Message = message }; } #endregion #region Success public static AjaxResult Success() { return new AjaxResult() { iserror = false }; } public static AjaxResult Success(string message) { return new AjaxResult() { iserror = false, Message = message }; } public static AjaxResult Success(object data) { return new AjaxResult() { iserror = false, Data = data }; } public static AjaxResult Success(object data, string message) { return new AjaxResult() { iserror = false, Data = data, Message = message }; } #endregion ////// 序列化 /// ///public override string ToString() { return new JavaScriptSerializer().Serialize(this); } }}
1.B.2,FormatJsonExtension.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Web;using System.Web.Mvc;using Newtonsoft.Json;namespace Core.WebHelper{ ////// 格式化json扩展 /// public static class FormatJsonExtension { ////// 普通序列化(不进行UI友好的json化) /// /// 控制器 /// 数据 ///public static FormatJsonResult JsonFormat(this Controller c, object data) { FormatJsonResult result = new FormatJsonResult(); result.NotLigerUIFriendlySerialize = true; result.Data = data; return result; } /// /// UI友好的json格式序列化 /// /// /// /// /// ///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)); } /// /// 成功的json返回 /// /// /// /// ///public static FormatJsonResult JsonFormatSuccess(this Controller c, object data, string message) { return JsonFormat(c, data, false, message); } /// /// 失败的json返回 /// /// /// ///public static FormatJsonResult JsonFormatError(this Controller c, string message) { return JsonFormat(c, null, true, message); } } /// /// JsonResult格式化扩展 /// 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; } ////// 正常序列化方式(为True则不进行UI友好的序列化) /// 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 { // Converters = new JsonConverter[] { new Newtonsoft.Json.Converters.IsoDateTimeConverter() }, 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()); } } ////// 系统的常见操作 /// public enum SysOperate { Add = 0, Update = 1, Load = 2, Delete = 3, Operate = 4, UnkownError = 5 } #region 友好的提示信息 ////// 友好的提示信息 /// public static class FriendlyMessage { //#region ////////// 获取消息(根据操作类型和状态) ///// ///// 操作类型 ///// 执行状态 ///////public static string ToMessage(this SysOperate sysOp, bool status) //{ // string message = ""; // //根据操作类型和执行状态返回消息 // switch (sysOp) // { // case SysOperate.Add: // message = status ? SystemMessage.AddSuccess.ToMessage() : SystemMessage.AddError.ToMessage(); // break; // case SysOperate.Load: // message = status ? SystemMessage.LoadSuccess.ToMessage() : SystemMessage.LoadError.ToMessage(); // break; // case SysOperate.Update: // message = status ? SystemMessage.UpdateSuccess.ToMessage() : SystemMessage.UpdateError.ToMessage(); // break; // case SysOperate.Delete: // message = status ? SystemMessage.DeleteSuccess.ToMessage() : SystemMessage.DeleteError.ToMessage(); // break; // case SysOperate.Operate: // message = status ? SystemMessage.OperateSuccess.ToMessage() : SystemMessage.OperateError.ToMessage(); // break; // case SysOperate.UnkownError: // message = SystemMessage.UnkownError.ToMessage(); // break; // } // return message; //} ///// ///// 获取系统管理模块友好提示信息 ///// ///// ///////public static string ToMessage(this SystemMessage code) //{ // string message = ""; // switch (code) // { // case SystemMessage.AddSuccess: // message = "添加成功!"; // break; // case SystemMessage.AddError: // message = "添加失败!"; // break; // case SystemMessage.DeleteSuccess: // message = "删除成功!"; // break; // case SystemMessage.DeleteError: // message = "删除失败!"; // break; // case SystemMessage.LoadSuccess: // message = "加载成功!"; // break; // case SystemMessage.LoadError: // message = "加载失败!"; // break; // case SystemMessage.OperateSuccess: // message = "操作成功!"; // break; // case SystemMessage.OperateError: // message = "操作失败!"; // break; // case SystemMessage.UpdateSuccess: // message = "更新成功!"; // break; // case SystemMessage.UpdateError: // message = "更新失败!"; // break; // case SystemMessage.UnkownError: // message = "未知错误!"; // break; // default: // message = "错误"; // break; // } // return message; //} //#endregion //#region 获取用户模块界面友好的消息提示 ///// ///// 获取用户模块界面友好的消息提示 ///// ///// ///////public static string ToMessage(this UserMessage code) //{ // string message = ""; // switch (code) // { // case UserMessage.UserNotLogin: // message = "用户未登录!"; // break; // case UserMessage.UserLoginSuccess: // message = "登录成功!"; // break; // case UserMessage.UserLoginError: // message = "用户登录失败!"; // break; // case UserMessage.ChangePasswordSuccess: // message = "修改密码成功!"; // break; // case UserMessage.ChangePasswordError: // message = "修改密码失败!"; // break; // case UserMessage.UnkonwnError: // message = "未知错误"; // break; // default: // message = "未知错误"; // break; // } // return message; //} //#endregion } #endregion}
1.B.3,
1.C,下载地址 |
作者:出处:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 |