先新建一个过滤器ExceptionHandleErrorAttribute.cs

内容如下:

 using System;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ABBPMP.Utility.NLogHelper.Static; namespace ABBPMP.Filter
{
/// <summary>
/// 异常捕获(业务逻辑层,UI层)
/// </summary>
public class ExceptionHandleErrorAttribute : HandleErrorAttribute
{
/// <summary>
/// 错误拦截
/// </summary>
/// <param name="filterContext"></param>
public override void OnException(ExceptionContext filterContext)
{ if (filterContext.ExceptionHandled)
{
return;
} string message =
$"消息类型:{filterContext.Exception.GetType().Name}\r\n消息内容:{filterContext.Exception.Message}\r\n引发异常的方法:{filterContext.Exception.TargetSite}\r\n引发异常的对象:{filterContext.Exception.Source}\r\n异常目录:{filterContext.RouteData.GetRequiredString("controller")}\r\n异常方法:{filterContext.RouteData.GetRequiredString("action")}\r\n错误详细记录:{filterContext.Exception.StackTrace}";
NLogHandler.Instance.Error(message);
if (!filterContext.HttpContext.Request.IsAjaxRequest())
{
filterContext.Controller.ViewData.Model = filterContext.Exception;
filterContext.Result = new ViewResult
{
ViewName = "~/Views/Error/Error.cshtml",
ViewData = filterContext.Controller.ViewData
};
}
filterContext.Result = AjaxError(filterContext.Exception.Message, filterContext); filterContext.ExceptionHandled = true;
}
/// <summary>
/// Ajaxes the error.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="filterContext">The filter context.</param>
/// <returns>JsonResult</returns>
protected JsonResult AjaxError(string message, ExceptionContext filterContext)
{ //If message is null or empty, then fill with generic message
if (String.IsNullOrEmpty(message))
message = "Something went wrong while processing your request. Please refresh the page and try again.";
//Set the response status code to 500
filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
//Needed for IIS7.0
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
return new JsonResult
{
//can extend more properties
Data = new AjaxExceptionModel() { ErrorMessage = message },
ContentEncoding = System.Text.Encoding.UTF8,
JsonRequestBehavior = JsonRequestBehavior.DenyGet }; }
/// <summary>
/// AjaxExceptionModel
/// </summary>
public class AjaxExceptionModel
{
/// <summary>
/// Gets or sets the error message.
/// </summary>
/// <value>
/// The error message.
/// </value>
public string ErrorMessage { get; set; } } }
}

然后在FilterConfig添加

Global.asax全局下添加

最后处理下ajax错误处理和服务器错误呈现形式

@{
ViewBag.Title = "General Site Error";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="container">
<div class="row text-center">
<br/>
<br />
<br />
<br />
<br />
<div class="col-md-6 col-md-offset-3 text-center">
@{ var exception = ViewData.Model;
var statusCode = exception == null ? 404 : 500;
Response.StatusCode = statusCode;
if (statusCode == 404)
{
<h1>404 Page not found!</h1>
<p>没有找到该网页!</p>
}
else if (statusCode == 500)
{
<h1>500 程序异常</h1>
<p>
<a class="btn" data-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample">
Error details
</a>
</p>
<div class="collapse" id="collapseExample">
<div class="card card-body">
<p style="text-align: left;">消息类型:@exception.GetType().Name<br />消息内容:@exception.Message <br />引发异常的方法:@exception.TargetSite <br />引发异常的对象:@exception.Source<br />错误详细记录:@exception.StackTrace</p>
</div>
</div> }
}
<p style="font-size: 14px; color: Gray">请使用浏览器的后退功能已保证您填写的数据没有丢失!</p>
</div>
</div> <div class="row text-center">
<div class="col-md-8 col-md-offset-2">
<h3> <i class="fa fa-lightbulb-o fa-5x"></i> </h3>
<a href="@Url.Action("Index","Home")" class="btn">GO TO HOME PAGE</a>
</div>
</div> </div>
 $(document).ajaxError(function (event, request, settings) {
//request.responseText
if (request.responseText != "") {
var jsonValue = jQuery.parseJSON(request.responseText);
}
toastr.error("<li>settings.url:" + settings.url + "</li>" + "<li>request.status:" + request.status + "</li>" + "<li>request.statusText:" + request.statusText + "</li>" + "<li>ErrorMessage:" + jsonValue.ErrorMessage + "</li>");
});

ASP.NET MVC 全局异常的更多相关文章

  1. asp.net mvc全局异常捕获

    通过重写OnException方法形式实现. 1.自定义异常记录类并继承HandleErrorAttribute类. public class HandlerErrorAttribute : Hand ...

  2. Asp.net Core全局异常监控和记录日志

    前言           系统异常监控可以说是重中之重,系统不可能一直运行良好,开发和运维也不可能24小时盯着系统,系统抛异常后我们应当在第一时间收到异常信息.在Asp.net Core里我使用拦截器 ...

  3. MVC 全局异常过滤器HandleErrorAttribute

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  4. ASP.NET MVC显示异常信息

    开发ASP.NET多了,它的异常信息显示也习惯了.但在ASP.NET MVC中,却是另外一番情形. 以前只习惯使用IE浏览器,现在开发ASP.NET MVC程序,为了捕获到异常信息,Firefox的f ...

  5. asp.net mvc 全局权限过滤器及继成权限方法

    全局权限过滤器 //----------------------------------------------------------------------- // <copyright f ...

  6. [Asp.net MVC]HandleErrorAttribute异常过滤器

    摘要 在asp.net mvc中除了使用try...catch/finally来处理异常外,它提供了一种通过在Controller或者Action上添加特性的方式来处理异常. HandleErrorA ...

  7. Spring MVC全局异常后返回JSON异常数据

    问题: 当前项目是作为手机APP后台支持,使用spring mvc + mybaits + shiro进行开发.后台服务与手机端交互是发送JSON数据.如果后台发生异常,会直接返回异常页面,显示异常内 ...

  8. asp.net core全局异常过滤并监控系统BUG将异常信息记录到日志

    添加: using Dw.Util.Helper; using Microsoft.AspNetCore.Mvc.Filters; using System; using System.Collect ...

  9. WinDBg定位asp.net mvc项目异常崩溃源码位置

    项目介绍:asp.net mvc + angular +iis(windows)+windows server 系统莫名崩溃 最近有个系统默认奇妙崩溃50x,服务整体变成无响应,当运维告知我只有重启应 ...

随机推荐

  1. mysql增加远程连接用户及查看数据库表结构

    一.增加远程连接用户 1.用root权限登录数据库  2.加用户:grant all privileges on *.* to '111'@'192.168.1.%' identified by '2 ...

  2. 123. Best Time to Buy and Sell Stock III (Array; DP)

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  3. Clustered Index & Non Clustered Index(聚簇索引和非聚簇索引)

    每个表只能有一个聚簇索引,而能有200多个非聚簇索引. 在物理分配上, 每个表的数据都是分配在页上,一个页大概有8k左右,假设一条数据占1000字节的话,那么8000条数据占8000*1k/8k = ...

  4. P3157 [CQOI2011]动态逆序对

    P3157 [CQOI2011]动态逆序对 https://www.luogu.org/problemnew/show/P3157 题目描述 对于序列A,它的逆序对数定义为满足i<j,且Ai&g ...

  5. swift - tableview 滚动到指定位置

    滚动一定要在  tableView.reloadData()之后进行 1. 默认  plain 模式 办法1. tableView.contentOffset.y = 0 办法2 tableView. ...

  6. Writing A Better JavaScript Library For The DOM 阅读记录

    原文地址:http://coding.smashingmagazine.com/2014/01/13/better-javascript-library-for-the-dom/ 主要观点: live ...

  7. dddddddddddddddddd

    PacificA: Replication in log-based distributed storage systemshttp://research.microsoft.com:8082/pub ...

  8. SQL 将一个字段内用逗号分隔的内容分成多条记录

    转自:http://www.cnblogs.com/zfanlong1314/archive/2013/01/14/2859848.html --> 测试数据 if not object_id( ...

  9. 神奇的照片修复术,这才是 PS 的正确打开方式!

    蒲公英种子从远处飘回 聚成伞的模样 太阳从西边升起 落向东方 运动员回到起跑线上 轰鸣的火车退回家乡 雪花纷飞 飘向天际 我沉入梦乡 你还在我身旁 ——公益广告 大概只有时光倒流,我们才能回到那些每天 ...

  10. DICOM

    DICOM(Digital Imaging and Communications in Medicine)即医学数字成像和通信,是医学图像和相关信息的国际标准(ISO 12052).它定义了质量能满足 ...