{"id":7291,"date":"2024-05-10T17:17:42","date_gmt":"2024-05-10T09:17:42","guid":{"rendered":"https:\/\/i007.vip:15443\/wordpress\/?p=7291"},"modified":"2024-05-10T17:17:42","modified_gmt":"2024-05-10T09:17:42","slug":"net-core%e4%b8%ad5%e7%a7%8d%e5%8f%8b%e5%a5%bd%e7%9a%84%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/7291","title":{"rendered":".Net Core\u4e2d5\u79cd\u53cb\u597d\u7684\u5f02\u5e38\u5904\u7406\u65b9\u6848"},"content":{"rendered":"<div class=\"article-header-box\">\n<div class=\"article-header\">\n<div class=\"article-title-box\">\n<p id=\"articleContentId\" class=\"title-article\"><a href=\"https:\/\/blog.csdn.net\/z123191456\/article\/details\/115954068\">cc from<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<article class=\"baidu_pl\">\n<div id=\"article_content\" class=\"article_content clearfix\">\n<div id=\"content_views\" class=\"markdown_views prism-atom-one-light\">\n<p>\u5f02\u5e38\u5904\u7406\u5728\u7f16\u7a0b\u4e2d\u975e\u5e38\u91cd\u8981\uff0c\u4e00\u6765\u53ef\u4ee5\u7ed9\u7528\u6237\u53cb\u597d\u63d0\u793a\uff0c\u4e8c\u6765\u4e5f\u662f\u4e3a\u4e86\u7a0b\u5e8f\u5b89\u5168\u3002\u5728Asp.Net Core\u4e2d\uff0c\u9ed8\u8ba4\u5df2\u7ecf\u4e3a\u6211\u4eec\u4e86\u63d0\u4f9b\u5f88\u591a\u89e3\u51b3\u65b9\u6848\uff0c\u4e0b\u9762\u5c31\u6765\u603b\u7ed3\u4e00\u4e0b\u3002<\/p>\n<h4><a name=\"t0\"><\/a><a id=\"ControllerOnActionExecuted_3\"><\/a>\u4e00\uff0c\u7ee7\u627fController\uff0c\u91cd\u5199OnActionExecuted<\/h4>\n<p>\u4f7f\u7528vs\u65b0\u5efacontroller\u65f6\uff0c\u9ed8\u8ba4\u90fd\u4f1a\u7ee7\u627f\u4e00\u4e2aController\u7c7b\uff0c\u91cd\u5199OnActionExecuted\uff0c\u6dfb\u52a0\u4e0a\u5f02\u5e38\u5904\u7406\u5373\u53ef\u3002\u4e00\u822c\u60c5\u51b5\u4e0b\u6211\u4eec\u4f1a\u65b0\u5efa\u4e00\u4e2aBaseController, \u8ba9\u6240\u6709Controller\u7ee7\u627fBaseController\u3002\u4ee3\u7801\u5982\u4e0b<\/p>\n<pre class=\"prettyprint set-code-show\" data-index=\"0\"><code class=\"prism language-html has-numbering\">public class BaseController : Controller\r\n{\r\n    public override void OnActionExecuted(ActionExecutedContext context)\r\n    {\r\n        var exception = context.Exception;\r\n        if (exception != null)\r\n        {\r\n            context.ExceptionHandled = true;\r\n            context.Result = new ContentResult\r\n            {\r\n                Content = $\"BaseController\u9519\u8bef : { exception.Message }\"\r\n            };\r\n        }\r\n        base.OnActionExecuted(context);\r\n    }\r\n}<\/code><\/pre>\n<p>\u8fd9\u79cd\u5904\u7406\u65b9\u5f0f\u4f18\u70b9\u5f53\u7136\u662f\u7b80\u5355\uff0c\u7f3a\u70b9\u4e5f\u5f88\u660e\u663e\uff0c\u5982\u679ccshtml\u9875\u9762\u629b\u9519\uff0c\u5c31\u5b8c\u5168\u6355\u83b7\u4e0d\u4e86\u3002\u5f53\u7136\u5982\u679c\u9879\u76ee\u672c\u8eab\u5c31\u662f\u4e00\u4e2aweb api \u9879\u76ee\uff0c\u6ca1\u6709view\uff0c\u8fd9\u8fd8\u662f\u53ef\u4ee5\u7684\u3002<\/p>\n<h4><a name=\"t1\"><\/a><a id=\"_ActionFilterAttribute_27\"><\/a>\u4e8c\uff0c\u4f7f\u7528 ActionFilterAttribute<\/h4>\n<p>ActionFilterAttribute\u662f\u4e00\u4e2a\u7279\u6027\uff0c\u672c\u8eab\u5b9e\u73b0\u4e86 IActionFilter \u53ca IResultFilter , \u6240\u4ee5\u4e0d\u7ba1\u662faction\u91cc\u629b\u9519\uff0c\u8fd8\u662fview\u91cc\u629b\u9519\uff0c\u7406\u8bba\u4e0a\u90fd\u53ef\u4ee5\u6355\u83b7\u3002\u6211\u4eec\u65b0\u5efa\u4e00\u4e2a ExceptionActionFilterAttribute, \u91cd\u5199 OnActionExecuted\u53caOnResultExecuted\uff0c\u6dfb\u52a0\u4e0a\u5f02\u5e38\u5904\u7406\uff0c\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\u3002<\/p>\n<pre class=\"prettyprint set-code-show\" data-index=\"1\"><code class=\"prism language-html has-numbering\">public class ExceptionActionFilterAttribute:ActionFilterAttribute\r\n{\r\n    public override void OnActionExecuted(ActionExecutedContext context)\r\n    {\r\n        var exception = context.Exception;\r\n        if (exception != null)\r\n        {\r\n            context.ExceptionHandled = true;\r\n            context.Result = new ContentResult\r\n            {\r\n                Content = $\"\u9519\u8bef : { exception.Message }\"\r\n            };\r\n        }\r\n        base.OnActionExecuted(context);\r\n    }\r\n\r\n    public override void OnResultExecuted(ResultExecutedContext context)\r\n    {\r\n        var exception = context.Exception;\r\n        if (exception != null)\r\n        {\r\n            context.ExceptionHandled = true;\r\n            context.HttpContext.Response.WriteAsync($\"\u9519\u8bef : {exception.Message}\");\r\n        }\r\n        base.OnResultExecuted(context);\r\n    }\r\n}<\/code><\/pre>\n<p>\u4f7f\u7528\u65b9\u5f0f\u6709\u4e24\u79cd\uff0c<\/p>\n<p>\u5728controller\u91cc\u6253\u4e0a [TypeFilter(typeof(ExceptionActionFilter)] \u6807\u7b7e\u3002<br \/>\n\u5728Startup\u91cc\u4ee5filter\u65b9\u5f0f\u5168\u5c40\u6ce8\u5165\u3002<\/p>\n<pre class=\"set-code-show prettyprint\" data-index=\"2\"><code class=\"prism language-html has-numbering\">services.AddControllersWithViews(options =&gt;\r\n{\r\n    options.Filters.Add<span class=\"token tag\"><span class=\"token punctuation\">&lt;<\/span>ExceptionActionFilterAttribute<span class=\"token punctuation\">&gt;<\/span><\/span>();\r\n})<\/code><\/pre>\n<h4><a name=\"t2\"><\/a><a id=\"_IExceptionFilter_73\"><\/a>\u4e09\uff0c\u4f7f\u7528 IExceptionFilter<\/h4>\n<p>\u6211\u4eec\u77e5\u9053, Asp.Net Core\u63d0\u4f9b\u4e865\u7c7bfilter, IExceptionFilter\u662f\u5176\u4e2d\u4e4b\u4e00\uff0c\u987e\u540d\u601d\u4e49\uff0c\u8fd9\u5c31\u662f\u7528\u6765\u5904\u7406\u5f02\u5e38\u7684\u3002Asp.net Core\u4e2dExceptionFilterAttribute\u5df2\u7ecf\u5b9e\u73b0\u4e86IExceptionFilter\uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u9700\u7ee7\u627fExceptionFilterAttribute\uff0c\u91cd\u5199\u5176\u4e2d\u65b9\u6cd5\u5373\u53ef\u3002 \u540c\u6837\u65b0\u5efaCustomExceptionFilterAttribute\u7ee7\u627f ExceptionFilterAttribute\uff0c\u91cd\u5199 OnException \uff0c\u6dfb\u52a0\u5f02\u5e38\u5904\u7406\uff0c\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b<\/p>\n<pre class=\"set-code-show prettyprint\" data-index=\"3\"><code class=\"prism language-html has-numbering\">public class CustomExceptionFilterAttribute : ExceptionFilterAttribute\r\n{\r\n    public override void OnException(ExceptionContext context)\r\n    {\r\n        context.ExceptionHandled = true;\r\n        context.HttpContext.Response.WriteAsync($\"CustomExceptionFilterAttribute\u9519\u8bef:{context.Exception.Message}\");\r\n        base.OnException(context);\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>\u6ce8\u610f\uff0cExceptionFilterAttribute\u8fd8\u63d0\u4f9b\u4e86\u5f02\u6b65\u65b9\u6cd5OnExceptionAsync\uff0c\u8fd9\u4e24\u4e2a\u5904\u7406\u65b9\u6cd5\uff0c\u53ea\u9700\u91cd\u5199\u4e00\u4e2a\u5373\u53ef\uff0c\u5982\u679c\u4e24\u4e2a\u90fd\u91cd\u5199\uff0c\u4e24\u4e2a\u65b9\u6cd5\u90fd\u4f1a\u6267\u884c\u4e00\u6b21\u3002<\/p>\n<p>\u4f7f\u7528\u65b9\u5f0f\u6709\u4e24\u79cd\uff0c<\/p>\n<p>\u5728controller\u91cc\u6253\u4e0a [CustomExceptionFilter] \u6807\u7b7e\u3002<br \/>\n\u5728Startup\u91cc\u4ee5filter\u65b9\u5f0f\u5168\u5c40\u6ce8\u5165\u3002<\/p>\n<pre class=\"set-code-show prettyprint\" data-index=\"4\"><code class=\"prism language-html has-numbering\">services.AddControllersWithViews(options =&gt;\r\n{\r\n    options.Filters.Add<span class=\"token tag\"><span class=\"token punctuation\">&lt;<\/span>CustomExceptionFilterAttribute<span class=\"token punctuation\">&gt;<\/span><\/span>();\r\n})<\/code><\/pre>\n<h4><a name=\"t3\"><\/a><a id=\"ExceptionHandler_103\"><\/a>\u56db\uff0c\u4f7f\u7528ExceptionHandler<\/h4>\n<p>\u5728 startup \u91cc\uff0cvs\u65b0\u5efa\u7684\u9879\u76ee\u4f1a\u9ed8\u8ba4\u52a0\u4e0a<\/p>\n<pre class=\"set-code-show prettyprint\" data-index=\"5\"><code class=\"prism language-html has-numbering\">if (env.IsDevelopment())\r\n{\r\n    app.UseDeveloperExceptionPage();\r\n}\r\nelse\r\n{\r\n    app.UseExceptionHandler(\"\/Home\/Error\");\r\n}\r\n<\/code><\/pre>\n<p>\u4e0a\u8fb9\u8fd9\u6bb5\u4ee3\u7801\uff0c\u5927\u610f\u4e3a\uff1a\u5f00\u53d1\u73af\u5883\u4f7f\u7528app.UseDeveloperExceptionPage(); \u3002\u751f\u4ea7\u73af\u5883\u4f1a\u8df3\u8f6c\u81f3 home\/error \u9875\u9762\u3002<br \/>\nUseDeveloperExceptionPage \u4f1a\u7ed9\u51fa\u5177\u4f53\u9519\u8bef\u4fe1\u606f\uff0c\u5177\u4f53\u5305\u62ec Stack trace\u3001Query\u3001Cookies\u3001Headers \u56db\u90e8\u5206\u3002.net core 3.1\u540e\u6709\u4e9b\u6539\u52a8\uff0c\u5982\u679c\u8bf7\u6c42\u5934\u52a0\u4e0a accept:text\/html, \u5c31\u8fd4\u56dehtml\u9875\u9762\uff0c\u4e0d\u52a0\u7684\u8bdd\uff0c\u53ea\u4f1a\u8fd4\u56de\u9519\u8bef\u3002<\/p>\n<p>\u6ce8\u610f\uff1aapp.UseDeveloperExceptionPage(); \u5e94\u8be5\u7f6e\u4e8e\u6700\u524d\u8fb9\u3002<\/p>\n<pre class=\"set-code-show prettyprint\" data-index=\"6\"><code class=\"prism language-html has-numbering\">if (!env.IsDevelopment())\r\n {\r\n     app.UseDeveloperExceptionPage();\r\n }\r\n else\r\n {\r\n     app.UseExceptionHandler(builder =&gt;\r\n     {\r\n         builder.Run(async context =&gt;\r\n         {\r\n             var exceptionHandlerPathFeature = context.Features.Get<span class=\"token tag\"><span class=\"token punctuation\">&lt;<\/span>IExceptionHandlerPathFeature<span class=\"token punctuation\">&gt;<\/span><\/span>();\r\n             await context.Response.WriteAsync($\"error:{exceptionHandlerPathFeature.Error.Message}\");\r\n         });\r\n     });\r\n }\r\n<\/code><\/pre>\n<p>\u4e94\uff0c\u81ea\u5b9a\u4e49Middleare\u5904\u7406<br \/>\n\u901a\u8fc7middleware\u5168\u5c40\u5904\u7406\u3002<\/p>\n<pre class=\"prettyprint set-code-show\" data-index=\"7\"><code class=\"prism language-html has-numbering\">public class ErrorHandlingMiddleware\r\n{\r\n   private readonly RequestDelegate next;\r\n\r\n   public ErrorHandlingMiddleware(RequestDelegate next)\r\n   {\r\n        this.next = next;\r\n   }\r\n\r\n   public async Task Invoke(HttpContext context)\r\n   {\r\n        try\r\n        {\r\n           await next(context);\r\n        }\r\n        catch (System.Exception ex)\r\n        {\r\n           \/\/\u5904\u7406\u5f02\u5e38\r\n        }\r\n   }\r\n}<\/code><\/pre>\n<pre class=\"prettyprint set-code-show\" data-index=\"7\"><code class=\"prism language-html has-numbering\"><\/code><\/pre>\n<\/div>\n<\/div>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>cc from \u5f02\u5e38\u5904\u7406\u5728\u7f16\u7a0b\u4e2d\u975e\u5e38\u91cd\u8981<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[24],"tags":[121],"class_list":["post-7291","post","type-post","status-publish","format-standard","hentry","category-value_docs","tag-05-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/7291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=7291"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/7291\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=7291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=7291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=7291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}