ASP.NET MVC ajax處理 AjaxResult

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   sp   資料   

1、統一ASPNET MVC 對ajax請求響應格式定義,方便前端統一處理ajax結果.

  1)定義程式返回結果資料格式

 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

ASP.NET MVC ajax處理 AjaxResult

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.