forked from siteserver/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorRedirectModule.cs
More file actions
92 lines (78 loc) · 3.24 KB
/
ErrorRedirectModule.cs
File metadata and controls
92 lines (78 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Web;
using SiteServer.CMS.Core;
namespace SiteServer.API
{
public class ErrorRedirectModule : IHttpModule
{
public string ModuleName => "ErrorRedirectModule";
public void Init(HttpApplication app)
{
app.Error += Application_Error;
}
private static void Application_Error(object sender, EventArgs e)
{
try
{
var lastError = HttpContext.Current.Server.GetLastError();
if (lastError != null)
{
var httpError = lastError as HttpException;
if (httpError != null)
{
//ASP.NET的400与404错误不记录日志,并都以自定义404页面响应
var httpCode = httpError.GetHttpCode();
if (httpCode == 400 || httpCode == 404)
{
HttpContext.Current.Response.TrySkipIisCustomErrors = true;
HttpContext.Current.Response.StatusCode = 404;//在IIS中配置自定义404页面
HttpContext.Current.Server.ClearError();
return;
}
}
//对于路径错误不记录日志,并都以自定义404页面响应
if (lastError.TargetSite.ReflectedType == typeof(System.IO.Path))
{
HttpContext.Current.Response.TrySkipIisCustomErrors = true;
HttpContext.Current.Response.StatusCode = 404;
HttpContext.Current.Server.ClearError();
return;
}
if (lastError.InnerException != null)
{
lastError = lastError.InnerException;
}
HttpContext.Current.Server.ClearError();
LogUtils.AddErrorLogAndRedirect(lastError, "Server Error in Application");
}
//var ex = HttpContext.Current.Server.GetLastError();
//if (ex != null)
//{
// var httpError = ex as HttpException;
// if (httpError != null && httpError.ErrorCode == 404)
// {
// var response = HttpContext.Current.Response;
// response.TrySkipIisCustomErrors = true; // For IIS 7 Integrated Pipeline - see previous post
// response.Status = "404 Not Found";
// response.StatusCode = 404;
// HttpContext.Current.Server.ClearError();
// return;
// }
// if (ex.InnerException != null)
// {
// ex = ex.InnerException;
// }
// HttpContext.Current.Server.ClearError();
// LogUtils.AddErrorLogAndRedirect(ex, "Server Error in Application");
//}
}
catch
{
// ignored
}
}
public void Dispose()
{
}
}
}