ASP.NET MVC 全局异常
程序员文章站
2023-11-07 20:13:46
先新建一个过滤器ExceptionHandleErrorAttribute.cs 内容如下: 1 using System; 2 using System.Net; 3 using System.Web; 4 using System.Web.Mvc; 5 using ABBPMP.Utility. ......
先新建一个过滤器exceptionhandleerrorattribute.cs
内容如下:
1 using system; 2 using system.net; 3 using system.web; 4 using system.web.mvc; 5 using abbpmp.utility.nloghelper.static; 6 7 namespace abbpmp.filter 8 { 9 /// <summary> 10 /// 异常捕获(业务逻辑层,ui层) 11 /// </summary> 12 public class exceptionhandleerrorattribute : handleerrorattribute 13 { 14 /// <summary> 15 /// 错误拦截 16 /// </summary> 17 /// <param name="filtercontext"></param> 18 public override void onexception(exceptioncontext filtercontext) 19 { 20 21 if (filtercontext.exceptionhandled) 22 { 23 return; 24 } 25 26 27 28 string message = 29 $"消息类型:{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}"; 30 nloghandler.instance.error(message); 31 if (!filtercontext.httpcontext.request.isajaxrequest()) 32 { 33 filtercontext.controller.viewdata.model = filtercontext.exception; 34 filtercontext.result = new viewresult 35 { 36 viewname = "~/views/error/error.cshtml", 37 viewdata = filtercontext.controller.viewdata 38 }; 39 } 40 filtercontext.result = ajaxerror(filtercontext.exception.message, filtercontext); 41 42 43 44 45 filtercontext.exceptionhandled = true; 46 } 47 /// <summary> 48 /// ajaxes the error. 49 /// </summary> 50 /// <param name="message">the message.</param> 51 /// <param name="filtercontext">the filter context.</param> 52 /// <returns>jsonresult</returns> 53 protected jsonresult ajaxerror(string message, exceptioncontext filtercontext) 54 { 55 56 //if message is null or empty, then fill with generic message 57 if (string.isnullorempty(message)) 58 message = "something went wrong while processing your request. please refresh the page and try again."; 59 //set the response status code to 500 60 filtercontext.httpcontext.response.statuscode = (int)httpstatuscode.internalservererror; 61 //needed for iis7.0 62 filtercontext.httpcontext.response.tryskipiiscustomerrors = true; 63 return new jsonresult 64 { 65 //can extend more properties 66 data = new ajaxexceptionmodel() { errormessage = message }, 67 contentencoding = system.text.encoding.utf8, 68 jsonrequestbehavior = jsonrequestbehavior.denyget 69 70 }; 71 72 } 73 /// <summary> 74 /// ajaxexceptionmodel 75 /// </summary> 76 public class ajaxexceptionmodel 77 { 78 /// <summary> 79 /// gets or sets the error message. 80 /// </summary> 81 /// <value> 82 /// the error message. 83 /// </value> 84 public string errormessage { get; set; } 85 86 } 87 88 } 89 }
然后在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 Core全局异常监控和记录日志
-
浅析Asp.net MVC 中Ajax的使用
-
C# MVC 全局错误Application_Error中处理(包括Ajax请求)
-
spring boot 2 全局统一返回RESTful风格数据、统一异常处理
-
ASP.NET MVC中jQuery与angularjs混合应用传参并绑定数据
-
Asp.net MVC 对所有用户输入的字符串字段做Trim处理的方法
-
ASP.NET Core异常和错误处理(8)
-
ASP.NET MVC异步获取和刷新ExtJS6 TreeStore
-
ASP.NET MVC5+EF6+EasyUI 后台管理系统(81)-数据筛选(万能查询)实例
-
Asp.Net MVC中配置Serilog的方法