我是能够得到解决通过使用.NET 3.5的asp.net webforms中的以下设置来解决此问题。
我实现的模式绕过在web.config .NET的自定义重定向解决方案,我写我自己来处理所有的场景头中具有正确的HTTP状态代码。
首先,web.config中的部分的customErrors看起来是这样的:
<customErrors mode="RemoteOnly" defaultRedirect="~/error.htm" />
这种设置保证的customErrors模式设置为上,后来我们就需要设置,并提供了一个所有其他人,失败选项为error.htm的defaultRedirect。这会派上用场的时候我没有特定的错误处理程序,或有沿着破碎的数据库连接线的东西。
其次,这里是全球ASAX错误事件:
protected void Application_Error(object sender, EventArgs e)
{
HandleError();
}
private void HandleError()
{
var exception = Server.GetLastError();
if (exception == null) return;
var baseException = exception.GetBaseException();
bool errorHandled = _applicationErrorHandler.HandleError(baseException);
if (!errorHandled) return;
var lastError = Server.GetLastError();
if (null != lastError && HttpContext.Current.IsCustomErrorEnabled)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(lastError.GetBaseException());
Server.ClearError();
}
}
此代码是假冒的处理错误给另一大类责任。如果不处理的错误,的customErrors被打开,这意味着我们已经有了,我们是生产和莫名其妙的错误没有被处理的情况。我们会在这里清除,以防止用户看到它,但它登录在ELMAH所以我们知道这是怎么回事。
的applicationErrorHandler类看起来是这样的:
public bool HandleError(Exception exception)
{
if (exception == null) return false;
var baseException = exception.GetBaseException();
Elmah.ErrorSignal.FromCurrentContext().Raise(baseException);
if (!HttpContext.Current.IsCustomErrorEnabled) return false;
try
{
var behavior = _responseBehaviorFactory.GetBehavior(exception);
if (behavior != null)
{
behavior.ExecuteRedirect();
return true;
}
}
catch (Exception ex)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}
return false;
}
该类基本上使用命令模式来定位该款项核发错误的类型相应的错误处理程序。在这个级别使用Exception.GetBaseException()是很重要的,因为几乎每个错误都会被封装在一个更高级别的异常中。例如,从任何aspx页面执行“throw new System.Exception()”将导致在此级别接收到HttpUnhandledException,而不是System.Exception。
“工厂”的代码很简单,看起来像这样:
public ResponseBehaviorFactory()
{
_behaviors = new Dictionary<Type, Func<IResponseBehavior>>
{
{typeof(StoreException),() => new Found302StoreResponseBehavior()},
{typeof(HttpUnhandledException),() => new HttpExceptionResponseBehavior()},
{typeof(HttpException),() => new HttpExceptionResponseBehavior()},
{typeof(Exception),() => new Found302DefaultResponseBehavior()}
};
}
public IResponseBehavior GetBehavior(Exception exception)
{
if (exception == null) throw new ArgumentNullException("exception");
Func<IResponseBehavior> behavior;
bool tryGetValue = _behaviors.TryGetValue(exception.GetType(), out behavior);
//default value here:
if (!tryGetValue)
_behaviors.TryGetValue(typeof(Exception), out behavior);
if (behavior == null)
Elmah.ErrorSignal.FromCurrentContext().Raise(
new Exception(
"Danger! No Behavior defined for this Exception, therefore the user might have received a yellow screen of death!",
exception));
return behavior();
}
最后,我有一个可扩展的错误处理方案设置。在每个定义的“行为”中,我都有一个针对错误类型的自定义实现。例如,将检查一个Http异常的状态代码并正确处理。 404状态代码将需要Server.Transfer而不是Request.Redirect,以及写入标题中的相应状态代码。
希望这会有所帮助。
浏览器状态如何?我使用Firefox的插件Header Spy。 – 2008-12-07 06:17:25
标头间谍响应: HTTP/1.1 404未找到 日期:Sun,07 Dec 2008 06:21:20 GMT – 2008-12-07 06:21:54
您是否正在使用母版页?也许就是这样。我会尝试一个页面,而不使用母版页... – 2008-12-07 06:23:12