{"id":189,"date":"2023-06-14T09:11:09","date_gmt":"2023-06-14T01:11:09","guid":{"rendered":"https:\/\/www.gzlcy.cn\/?p=189"},"modified":"2023-06-14T09:11:09","modified_gmt":"2023-06-14T01:11:09","slug":"c-mvc-%e9%94%99%e8%af%af%e8%b7%b3%e8%bd%ac%e6%8c%87%e5%ae%9a404-%e9%a1%b5%e9%9d%a2","status":"publish","type":"post","link":"http:\/\/www.gzlcy.cn\/?p=189","title":{"rendered":"C# MVC \u9519\u8bef\u8df3\u8f6c\u6307\u5b9a404 \u9875\u9762"},"content":{"rendered":"<p>\u524d\u8a00<\/p>\n<p>\u8df3\u8f6c\u65b9\u6cd5\u6709\u4e24\u79cd\uff0c\u7b2c\u4e00\u79cd\u662f\u4fee\u6539\u914d\u7f6e\u6587\u4ef6\uff0c\u7b2c\u4e8c\u4e2d\u662f\u5728Global.assx\u4e2d\u6dfb\u52a0Application_Error\u5904\u7406\u65b9\u6cd5\u3002<\/p>\n<p>\u65b9\u6cd5\u4e00\u6b65\u9aa4\uff1a(Application_Error)<\/p>\n<p>\u4e00\u3001\u5148\u5173\u95ed\u81ea\u5b9a\u4e49\u9519\u8bef\uff0c\u5c06Web.config\u914d\u7f6e\u6587\u4ef6\u4e2dcustomErrors\u8282\u70b9\u7684mode\u8bbe\u7f6e\u4e3aOff\uff0c\u6ce8\u610f\u5927\u5c0f\u5199<\/p>\n<p><pre><code>     \n    \n    \n    \n  <\/code><\/pre>\n<\/p>\n<p>\u4e8c\u3001\u53d6\u6d88\u5728GlobalFilterGlobalFilterGlobalFilter\u5168\u5c40\u8fc7\u6ee4\u5668\u4e2d\u53d6\u6d88HandleErrorAttribute\u7684\u6ce8\u518c\uff1a<\/p>\n<p><pre><code>public class FilterConfig\n{\n    public static void RegisterGlobalFilters(GlobalFilterCollection filters)\n    {\n        \/\/filters.Add(new HandleErrorAttribute());\n    }\n}<\/code><\/pre>\n<\/p>\n<p>\u4e09\u3001\u5728Golbal.assx\u91cd\u5199Application_Error\u5904\u7406\u65b9\u6cd5<\/p>\n<p><pre><code>protected void Application_Error(Object sender, EventArgs e)\n{\n    Exception lastError = Server.GetLastError();\n    if (lastError != null)\n    {\n        \/\/\u5f02\u5e38\u4fe1\u606f\n        string strExceptionMessage = string.Empty;\n        \/\/\u5bf9HTTP 404\u505a\u989d\u5916\u5904\u7406\uff0c\u5176\u4ed6\u9519\u8bef\u5168\u90e8\u5f53\u6210500\u670d\u52a1\u5668\u9519\u8bef\n        HttpException httpError = lastError as HttpException;\n        if (httpError != null)\n        {\n            \/\/\u83b7\u53d6\u9519\u8bef\u4ee3\u7801\n            int httpCode = httpError.GetHttpCode();\n            strExceptionMessage = httpError.Message;\n            if (httpCode == 400 || httpCode == 404)\n            {\n                Response.StatusCode = 404;\n                \/\/\u8df3\u8f6c\u5230\u6307\u5b9a\u7684\u9759\u6001404\u4fe1\u606f\u9875\u9762\uff0c\u6839\u636e\u9700\u6c42\u81ea\u5df1\u66f4\u6539URL\n                Response.WriteFile(\"~\/HttpError\/404.html\");\n                Server.ClearError();\n                return;\n            }\n        }\n        strExceptionMessage = lastError.Message;\n        \/*-----------------------------------------------------\n         * \u6b64\u5904\u4ee3\u7801\u53ef\u6839\u636e\u9700\u6c42\u8fdb\u884c\u65e5\u5fd7\u8bb0\u5f55\uff0c\u6216\u8005\u5904\u7406\u5176\u4ed6\u4e1a\u52a1\u6d41\u7a0b\n         * ---------------------------------------------------*\/\n        \/*\n         * \u8df3\u8f6c\u5230\u6307\u5b9a\u7684http 500\u9519\u8bef\u4fe1\u606f\u9875\u9762\n         * \u8df3\u8f6c\u5230\u9759\u6001\u9875\u9762\u4e00\u5b9a\u8981\u7528Response.WriteFile\u65b9\u6cd5                 \n         *\/\n        Response.StatusCode = 500;\n        Response.WriteFile(\"~\/HttpError\/500.html\");\n        \/\/\u4e00\u5b9a\u8981\u8c03\u7528Server.ClearError()\u5426\u5219\u4f1a\u89e6\u53d1\u9519\u8bef\u8be6\u60c5\u9875\uff08\u5c31\u662f\u9ec4\u9875\uff09\n        Server.ClearError();\n        Server.Transfer(\"~\/HttpError\/500.aspx\");\n    }\n}<\/code><\/pre>\n<\/p>\n<p>\u7f16\u5199Application_Error\u4e8b\u4ef6\u7684\u4ee3\u7801\u9700\u8981\u6ce8\u610f\u7684\u5730\u65b9<\/p>\n<p>1\u3001\u4e00\u5b9a\u8981\u53d6\u6d88GlobalFilter\u5168\u5c40\u8fc7\u6ee4\u5668\u4e2dHandleErrorAttribute\u7684\u6ce8\u518c\uff0c\u4e5f\u8981\u6ce8\u610f\u68c0\u67e5\u9879\u76ee\u4e2d\u662f\u5426\u6709\u5176\u4ed6\u5168\u5c40\u8fc7\u6ee4\u5668\u5bf9\u5f02\u5e38\u8fdb\u884c\u5904\u7406\u7684\uff0c\u9632\u6b62HTTP 500\u7c7b\u578b\u7684\u670d\u52a1\u5668\u9519\u8bef\u4e0d\u4f1a\u89e6\u53d1Application_Error\u4e8b\u4ef6\uff08\u5176\u4ed6\u7c7b\u578b\u9519\u8bef\u4f9d\u7136\u53ef\u89e6\u53d1\uff09\u3002<\/p>\n<p>2\u3001\u65e0\u8bba\u6700\u7ec8\u5904\u7406\u7684\u6d41\u7a0b\u5982\u4f55\uff0c\u5728\u6d41\u7a0b\u7ed3\u675f\u6216\u8005\u54cd\u5e94\u8f93\u51fa\u7684\u5730\u65b9\uff0c\u4e00\u5b9a\u8981\u8c03\u7528Server.ClearError()\u65b9\u6cd5\u6e05\u7a7a\u5f02\u5e38\uff0c\u5426\u5219\u5f02\u5e38\u9519\u8bef\u4f9d\u7136\u5904\u4e8e\u672a\u88ab\u5904\u7406\u7684\u72b6\u6001\uff0c\u5982\u679ccustomErrors mode=&#8221;On&#8221;\uff0c\u90a3\u4e48\u5f02\u5e38\u4f1a\u88ab\u81ea\u5b9a\u4e49\u9519\u8bef\u6a21\u5757\u5904\u7406\uff0c\u9664\u975e\u672c\u610f\u5c31\u662f\u8981\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u8df3\u8f6c\u5230\u9519\u8bef\u9875\u3002<\/p>\n<p>3\u3001\u5982\u679c\u8981\u8df3\u8f6c\u5230\u9759\u6001\u7684\u81ea\u5b9a\u4e49\u9519\u8bef\u9875\u9762\u4e2d\uff0c\u8bf7\u4f7f\u7528Response.WriteFile(string filename)\u65b9\u6cd5\uff0c\u6700\u540e\u8bbe\u7f6e\u4e0bHTTP\u72b6\u6001\u7801<\/p>\n<p>4\u3001\u5982\u679c\u60f3\u8981\u4f7f\u7528Server.Transfer(string path)\u65b9\u6cd5\u8df3\u8f6c\u5230\u81ea\u5b9a\u4e49\u7684\u9519\u8bef\u9875\u9762\uff0c\u8fd9\u91cc\u6709\u4e24\u70b9\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00\u8df3\u8f6c\u65b9\u6cd5\u6709\u4e24\u79cd\uff0c\u7b2c\u4e00\u79cd\u662f\u4fee\u6539\u914d\u7f6e\u6587\u4ef6\uff0c\u7b2c\u4e8c\u4e2d\u662f\u5728Global.assx\u4e2d\u6dfb\u52a0Application_Error\u5904\u7406\u65b9\u6cd5\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[320],"_links":{"self":[{"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=\/wp\/v2\/posts\/189"}],"collection":[{"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=189"}],"version-history":[{"count":0,"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=\/wp\/v2\/posts\/189\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=189"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}