mvc控制器返回操作结果封装
实体类
public class AjaxResult { /// <summary> /// 获取 Ajax操作结果类型 /// </summary> public ResultType type { get; set; } /// <summary> /// 获取 Ajax操作结果编码 /// </summary> public int errorcode { get; set; } /// <summary> /// 获取 消息内容 /// </summary> public string message { get; set; } /// <summary> /// 获取 返回数据 /// </summary> public object resultdata { get; set; } } /// <summary> /// 表示 ajax 操作结果类型的枚举 /// </summary> public enum ResultType { /// <summary> /// 消息结果类型 /// </summary> info = 0, /// <summary> /// 成功结果类型 /// </summary> success = 1, /// <summary> /// 警告结果类型 /// </summary> warning = 2, /// <summary> /// 异常结果类型 /// </summary> error = 3 }
josn的封装
/// <summary> /// Json操作 /// </summary> public static class Json { public static object ToJson(this string Json) { return Json == null ? null : JsonConvert.DeserializeObject(Json); } public static string ToJson(this object obj) { var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" }; return JsonConvert.SerializeObject(obj, timeConverter); } public static string ToJson(this object obj, string datetimeformats) { var timeConverter = new IsoDateTimeConverter { DateTimeFormat = datetimeformats }; return JsonConvert.SerializeObject(obj, timeConverter); } public static T ToObject<T>(this string Json) { return Json == null ? default(T) : JsonConvert.DeserializeObject<T>(Json); } public static List<T> ToList<T>(this string Json) { return Json == null ? null : JsonConvert.DeserializeObject<List<T>>(Json); } public static DataTable ToTable(this string Json) { return Json == null ? null : JsonConvert.DeserializeObject<DataTable>(Json); } public static JObject ToJObject(this string Json) { return Json == null ? JObject.Parse("{}") : JObject.Parse(Json.Replace(" ", "")); } }
调用实例
/// <summary> /// 登录 /// </summary> [HttpPost] public ActionResult LoginMvc(op_users login) {
//代码 return Content(new AjaxResult { type = ResultType.success, message = "登陆成功" }.ToJson()); }