ylbtech-Unitity: cs-JsonHelper |
AjaxResult.cs FormatJsonExtension.cs
1.A,效果图返回顶部 |
1.B,源代码返回顶部 |
1.B.1,AjaxResult.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Script.Serialization; namespace Core.JsonHelper { /// <summary> /// 前台Ajax请求的统一返回结果类 /// </summary> public class AjaxResult { private AjaxResult() { } private bool iserror = false; /// <summary> /// 是否产生错误 /// </summary> public bool IsError { get { return iserror; } } /// <summary> /// 错误信息,或者成功信息 /// </summary> public string Message { get; set; } /// <summary> /// 成功可能时返回的数据 /// </summary> 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 /// <summary> /// 序列化 /// </summary> /// <returns></returns> 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 { /// <summary> /// 格式化json扩展 /// </summary> public static class FormatJsonExtension { /// <summary> /// 普通序列化(不进行UI友好的json化) /// </summary> /// <param name="c">控制器</param> /// <param name="data">数据</param> /// <returns></returns> public static FormatJsonResult JsonFormat(this Controller c, object data) { FormatJsonResult result = new FormatJsonResult(); result.NotLigerUIFriendlySerialize = true; result.Data = data; return result; } /// <summary> /// UI友好的json格式序列化 /// </summary> /// <param name="c"></param> /// <param name="data"></param> /// <param name="IsError"></param> /// <param name="message"></param> /// <returns></returns> 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; } /// <summary> /// 根据操作和提供的数据判断执行状态 /// </summary> /// <param name="c">控制器</param> /// <param name="data">数据</param> /// <param name="op">操作类型(增删改查,等等)</param> /// <returns></returns> 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)); } /// <summary> /// 根据操作和提供的数据判断执行状态 /// </summary> /// <param name="c">控制器</param> /// <param name="data">数据</param> /// <param name="status">数据</param> /// <param name="op">操作类型(增删改查,等等)</param> /// <returns></returns> 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)); } /// <summary> /// 成功的json返回 /// </summary> /// <param name="c"></param> /// <param name="data"></param> /// <param name="message"></param> /// <returns></returns> public static FormatJsonResult JsonFormatSuccess(this Controller c, object data, string message) { return JsonFormat(c, data, false, message); } /// <summary> /// 失败的json返回 /// </summary> /// <param name="c"></param> /// <param name="message"></param> /// <returns></returns> public static FormatJsonResult JsonFormatError(this Controller c, string message) { return JsonFormat(c, null, true, message); } } /// <summary> /// JsonResult格式化扩展 /// </summary> public class FormatJsonResult : ActionResult { private bool iserror = false; /// <summary> /// 是否产生错误 /// </summary> public bool IsError { get { return iserror; } set { this.iserror = value; } } /// <summary> /// 错误信息,或者成功信息 /// </summary> public string Message { get; set; } /// <summary> /// 成功可能时返回的数据 /// </summary> public Object Data { get; set; } /// <summary> /// 正常序列化方式(为True则不进行UI友好的序列化) /// </summary> 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()); } } /// <summary> /// 系统的常见操作 /// </summary> public enum SysOperate { Add = 0, Update = 1, Load = 2, Delete = 3, Operate = 4, UnkownError = 5 } #region 友好的提示信息 /// <summary> /// 友好的提示信息 /// </summary> public static class FriendlyMessage { //#region ///// <summary> ///// 获取消息(根据操作类型和状态) ///// </summary> ///// <param name="sysOp">操作类型</param> ///// <param name="status">执行状态</param> ///// <returns></returns> //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; //} ///// <summary> ///// 获取系统管理模块友好提示信息 ///// </summary> ///// <param name="code"></param> ///// <returns></returns> //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 获取用户模块界面友好的消息提示 ///// <summary> ///// 获取用户模块界面友好的消息提示 ///// </summary> ///// <param name="code"></param> ///// <returns></returns> //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,下载地址返回顶部 |
作者:ylbtech 出处:http://ylbtech.cnblogs.com/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 |
时间: 2024-10-10 09:21:19