我是能夠得到解決通過使用.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