首页 > 代码库 > 出错处理

出错处理

#region 出错处理

protected override void OnException(ExceptionContext filterContext)
{
#region 写log
//LogWriter logWriter = new LogWriter();
//logWriter.Error("SMP", msgContent, category: "General");
#endregion

filterContext.ExceptionHandled = true;
string msgContent = filterContext.Exception.Message.ToString();

var statusCode = (int)HttpStatusCode.InternalServerError;
if (filterContext.Exception is HttpException)
{
statusCode = (filterContext.Exception as HttpException).GetHttpCode();
}
else if (filterContext.Exception is UnauthorizedAccessException)
{
//to prevent login prompt in IIS
// which will appear when returning 401.
statusCode = (int)HttpStatusCode.Forbidden;
}
var statusCodeName = ((HttpStatusCode)statusCode).ToString();
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
string content = msgContent;
filterContext.Result = Json(new { result = false, Content = content, Redirect = "" });
}
else
filterContext.Result = this.RedirectToAction("Error", "home", new { msg = statusCodeName, code = statusCode, content = msgContent });
return;
}

public ActionResult Error(string msg,string code,string content)
{
ViewBag.msg = msg;
ViewBag.code = code;
ViewBag.content = content;
return View();

}
#endregion

出错处理