-
public class HandlerErrorAttribute : HandleErrorAttribute
-
{
-
/// <summary>
-
/// 控制器方法中出現異常,會調用該方法捕獲異常
-
/// </summary>
-
/// <param name="context">提供使用</param>
-
public override void OnException(ExceptionContext context)
-
{
-
WriteLog(context);
-
base.OnException(context);
-
context.ExceptionHandled = true;
-
if (context.Exception is UserFriendlyException)
-
{
-
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.OK;
-
context.Result = new ContentResult { Content = new AjaxResult { type = ResultType.error, message = context.Exception.Message }.ToJson() };
-
}
-
else if (context.Exception is NoAuthorizeException)
-
{
-
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
-
if (!context.HttpContext.Request.IsAjaxRequest())
-
{
-
context.HttpContext.Response.RedirectToRoute("Default", new { controller = "Error", action = "Error401", errorUrl = context.HttpContext.Request.RawUrl });
-
}
-
else
-
{
-
context.Result = new ContentResult { Content = context.HttpContext.Request.RawUrl };
-
}
-
}
-
else
-
{
-
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
-
ExceptionMessage error = new ExceptionMessage(context.Exception);
-
var s = error.ToJson();
-
if (!context.HttpContext.Request.IsAjaxRequest())
-
{
-
context.HttpContext.Response.RedirectToRoute("Default", new { controller = "Error", action = "Error500", data = WebHelper.UrlEncode(s) });
-
}
-
else
-
{
-
context.Result = new ContentResult { Content = WebHelper.UrlEncode(s) };
-
}
-
}
-
}
-
-
/// <summary>
-
/// 寫入日誌(log4net)
-
/// </summary>
-
/// <param name="context">提供使用</param>
-
private void WriteLog(ExceptionContext context)
-
{
-
if (context == null)
-
return;
-
if (context.Exception is NoAuthorizeException || context.Exception is UserFriendlyException)
-
{
-
//友好錯誤提示,未受權錯誤提示,記錄警告日誌
-
LogHelper.Warn(context.Exception.Message);
-
}
-
else
-
{
-
//異常錯誤,
-
LogHelper.Error(context.Exception);
-
-
////TODO :寫入錯誤日誌到數據庫
-
}
-
}
-
}
-
/// <summary>
-
/// 異常錯誤信息
-
/// </summary>
-
[Serializable]
-
public class ExceptionMessage
-
{
-
public ExceptionMessage()
-
{
-
}
-
-
/// <summary>
-
/// 構造函數
-
/// 默認顯示異常頁面
-
/// </summary>
-
/// <param name="ex">異常對象</param>
-
public ExceptionMessage(Exception ex)
-
:this(ex, true)
-
{
-
-
}
-
/// <summary>
-
/// 構造函數
-
/// </summary>
-
/// <param name="ex">異常對象</param>
-
/// <param name="isShowException">是否顯示異常頁面</param>
-
public ExceptionMessage(Exception ex, bool isShowException)
-
{
-
MsgType = ex.GetType().Name;
-
Message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
-
StackTrace = ex.StackTrace.Length > 300 ? ex.StackTrace.Substring(0, 300) : ex.StackTrace;
-
Source = ex.Source;
-
Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
-
Assembly = ex.TargetSite.Module.Assembly.FullName;
-
Method = ex.TargetSite.Name;
-
-
ShowException = isShowException;
-
var request = HttpContext.Current.Request;
-
IP = Net.Ip;
-
UserAgent = request.UserAgent;
-
Path = request.Path;
-
HttpMethod = request.HttpMethod;
-
}
-
/// <summary>
-
/// 消息類型
-
/// </summary>
-
public string MsgType { get; set; }
-
-
/// <summary>
-
/// 消息內容
-
/// </summary>
-
public string Message { get; set; }
-
-
/// <summary>
-
/// 請求路徑
-
/// </summary>
-
public string Path { get; set; }
-
-
/// <summary>
-
/// 程序集名稱
-
/// </summary>
-
public string Assembly { get; set; }
-
-
/// <summary>
-
/// 異常參數
-
/// </summary>
-
public string ActionArguments { get; set; }
-
-
/// <summary>
-
/// 請求類型
-
/// </summary>
-
public string HttpMethod { get; set; }
-
-
/// <summary>
-
/// 異常堆棧
-
/// </summary>
-
public string StackTrace { get; set; }
-
-
/// <summary>
-
/// 異常源
-
/// </summary>
-
public string Source { get; set; }
-
-
/// <summary>
-
/// 服務器IP 端口
-
/// </summary>
-
public string IP { get; set; }
-
-
/// <summary>
-
/// 客戶端瀏覽器標識
-
/// </summary>
-
public string UserAgent { get; set; }
-
-
-
/// <summary>
-
/// 是否顯示異常界面
-
/// </summary>
-
public bool ShowException { get; set; }
-
-
/// <summary>
-
/// 異常發生時間
-
/// </summary>
-
public string Time { get; set; }
-
-
/// <summary>
-
/// 異常發生方法
-
/// </summary>
-
public string Method { get; set; }
-
}
-
(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);
至此,咱們發現其實MVC的異常處理,真的很簡單,只須要在過濾器中全局註冊以後,而後重寫OnException的方法,實現邏輯便可。關鍵是在於項目中Ajax請求,須要用統一的封裝方法。