ASP.NET MVC中异常Exception拦截的深入理解(2)

/// <summary> /// 用户友好异常 /// </summary> public class UserFriendlyException : Exception { public UserFriendlyException(string message) : base(message) { } }

在异常拦截关键代码中,我们发现友好异常(UserFriendlyException)其实是返回了一个结果对象AjaxResult,

ASP.NET MVC中异常Exception拦截的深入理解

AjaxResult对象的定义:

/// <summary> /// 表示Ajax操作结果 /// </summary> 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 }

四、Ajax请求异常时处理

在异常拦截的关键代码中,我们有看到,如果是ajax请求时,是执行不同的逻辑,这是因为ajax的请求,不能直接通过MVC的路由跳转,在请求时必须返回结果内容

ASP.NET MVC中异常Exception拦截的深入理解

然后在前端ajax的方法中,统一处理返回的错误,以下是我们项目中用到的ajax封装,对异常错误,进行了统一处理。

(function ($) { "use strict"; $.httpCode = { success: "1", fail: "3", }; // http 通信异常的时候调用此方法 $.httpErrorLog = function (msg) { console.log('=====>' + new Date().getTime() + '<====='); console.log(msg); }; // ajax请求错误处理 $.httpError = function (xhr, textStatus, errorThrown) { if (xhr.status == 401) { location.href = "/Error/Error401?errorUrl=" + xhr.responseText; } if (xhr.status == 404) { location.href = "/Error/Error404?errorUrl=" + xhr.responseText; } if (xhr.status == 500) { location.href = "/Error/Error500?data=" + xhr.responseText; } }; /* get请求方法(异步): * url地址, param参数, callback回调函数 beforeSend 请求之前回调函数, complete 请求完成之后回调函数 * 考虑到get请求一般将参数与url拼接一起传递,所以将param参数放置最后 * 返回AjaxResult结果对象 */ $.httpAsyncGet = function (url, callback, beforeSend, complete, param) { $.ajax({ url: url, data: param, type: "GET", dataType: "json", async: true, cache: false, success: function (data) { if ($.isFunction(callback)) callback(data); }, error: function (XMLHttpRequest, textStatus, errorThrown) { $.httpError(XMLHttpRequest, textStatus, errorThrown); }, beforeSend: function () { if (!!beforeSend) beforeSend(); }, complete: function () { if (!!complete) complete(); } }); }; /* get请求方法(同步): * url地址,param参数 * 返回实体数据对象 */ $.httpGet = function (url, param) { var res = {}; $.ajax({ url: url, data: param, type: "GET", dataType: "json", async: false, cache: false, success: function (data) { res = data; }, error: function (XMLHttpRequest, textStatus, errorThrown) { $.httpError(XMLHttpRequest, textStatus, errorThrown); }, }); return res; }; /* post请求方法(异步): * url地址, param参数, callback回调函数 beforeSend 请求之前回调函数, complete 请求完成之后回调函数 * 返回AjaxResult结果对象 */ $.httpAsyncPost = function (url, param, callback, beforeSend, complete) { $.ajax({ url: url, data: param, type: "POST", dataType: "json", async: true, cache: false, success: function (data) { if ($.isFunction(callback)) callback(data); }, error: function (XMLHttpRequest, textStatus, errorThrown) { $.httpError(XMLHttpRequest, textStatus, errorThrown); }, beforeSend: function () { if (!!beforeSend) beforeSend(); }, complete: function () { if (!!complete) complete(); } }); }; /* post请求方法(同步): * url地址,param参数, callback回调函数 * 返回实体数据对象 */ $.httpPost = function (url, param, callback) { $.ajax({ url: url, data: param, type: "POST", dataType: "json", async: false, cache: false, success: function (data) { if ($.isFunction(callback)) callback(data); }, error: function (XMLHttpRequest, textStatus, errorThrown) { $.httpError(XMLHttpRequest, textStatus, errorThrown); }, }); }, /* ajax异步封装: * type 请求类型, url地址, param参数, callback回调函数 * 返回实体数据对象 */ $.httpAsync = function (type, url, param, callback) { $.ajax({ url: url, data: param, type: type, dataType: "json", async: true, cache: false, success: function (data) { if ($.isFunction(callback)) callback(data); }, error: function (XMLHttpRequest, textStatus, errorThrown) { $.httpError(XMLHttpRequest, textStatus, errorThrown); }, }); }; })(jQuery);

五、总结

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdssdj.html