ASP.NET MVC ajax处理 AjaxResult_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > ASP.NET MVC ajax处理 AjaxResult

ASP.NET MVC ajax处理 AjaxResult

 2014/10/18 21:06:16  OneWorks  程序员俱乐部  我要评论(0)
  • 摘要:1、统一ASPNETMVC对ajax请求响应格式定义,方便前端统一处理ajax结果.1)定义程序返回结果数据格式1///<summary>2///执行结果3///</summary>4///<typeparamname="T"></typeparam>5publicclassRunResult<T>6{7///<summary>8///操作状态9///</summary>
  • 标签:.net ASP.NET MVC net Ajax

1、统一ASPNET MVC 对ajax请求响应格式定义,方便前端统一处理ajax结果.

  1)定义程序返回结果数据格式

class="code_img_closed" src="/Upload/Images/2014101821/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('34171330-c21a-4a9b-894b-9dc293e8b208',event)" src="/Upload/Images/2014101821/2B1B950FA3DF188F.gif" alt="" />
 1 /// <summary>
 2     /// 执行结果
 3     /// </summary>
 4     /// <typeparam name="T"></typeparam>
 5     public class RunResult<T>
 6     {
 7         /// <summary>
 8         /// 操作状态
 9         /// </summary>
10         public ResultStatus Status { get; set; }
11         /// <summary>
12         /// 操作异常
13         /// </summary>
14         public Exception Exception { get; set; }
15         /// <summary>
16         /// 数据结果
17         /// </summary>
18         public T Result { get; set; }
19         /// <summary>
20         /// 操作消息
21         /// </summary>
22         public string Message { get; set; }
23     }
View Code

  2)定义执行结果状态

/// <summary>
    /// 执行结果
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class RunResult<T>
    {
        /// <summary>
        /// 操作状态
        /// </summary>
        public ResultStatus Status { get; set; }
        /// <summary>
        /// 操作异常
        /// </summary>
        public Exception Exception { get; set; }
        /// <summary>
        /// 数据结果
        /// </summary>
        public T Result { get; set; }
        /// <summary>
        /// 操作消息
        /// </summary>
        public string Message { get; set; }
    }
View Code

   3)返回前端数据格式

  public class AjaxEntity<T>
    {
        [JsonProperty(PropertyName = "Success")]
        public bool Success { get; set; }

        [JsonProperty(PropertyName = "Message")]
        public string Message { get; set; }

        [JsonProperty(PropertyName = "Data")]
        public T Data { get; set; }
    }
View Code

  4)定义asp.net mvc 中 ajax ActionResult

 public class AjaxResult<T> : CustomResult<AjaxEntity<T>>
    {
        private readonly RunResult<T> _runResult;

        public AjaxResult(RunResult<T> runResult)
        {
            _runResult = runResult;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override AjaxEntity<T> GetObject()
        {
            AjaxEntity<T> entity = new AjaxEntity<T> { Data = _runResult.Result };
            if (_runResult.Result is bool)
            {
                entity.Success = _runResult.Status == ResultStatus.Success && Convert.ToBoolean(_runResult.Result);
            }
            else
            {
                entity.Success = _runResult.Status == ResultStatus.Success;
            }
            entity.Data = _runResult.Result;
            entity.Message = _runResult.Message;
            return entity;
        }
    }
View Code
发表评论
用户名: 匿名