对于任何展望未来的人,我通过在 global.asax 文件中使用以下代码解决了这个问题:
protected void Application_Error(object sender, EventArgs e) {
// Get Exception
var ex = Server.GetLastError();
// Looking for SqlTimeout, which is inner exception to HttpExecption
// when it occurs.
if (!typeof(HttpException).IsInstanceOfType(ex))
return;
// Clear response.
HttpContext.Current.Response.Clear();
// Check inner
if (ex.InnerException != null && typeof(SqlException).IsInstanceOfType(ex.InnerException)) {
// Clear error
Server.ClearError();
// Redirect to basic html error.
Server.Transfer("/site_down.html", false);
}
else {
// Send to general error page.
Response.Redirect("~/error/", true);
}
}