我在與上一個ASP.NET MVC應用程序,我已經部署了我的共享主機上的自定義錯誤的問題不顯示。我創建了一個ErrorController,並將以下代碼添加到Global.asax中以捕獲未處理的異常,將它們記錄下來,然後將控制權轉移給ErrorController以顯示自定義錯誤。此代碼是從here採取:ASP.NET MVC應用程序的自定義錯誤頁,共享託管環境
protected void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
Response.Clear();
HttpException httpEx = ex as HttpException;
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Error");
if (httpEx == null)
{
routeData.Values.Add("action", "Index");
}
else
{
switch (httpEx.GetHttpCode())
{
case 404:
routeData.Values.Add("action", "HttpError404");
break;
case 500:
routeData.Values.Add("action", "HttpError500");
break;
case 503:
routeData.Values.Add("action", "HttpError503");
break;
default:
routeData.Values.Add("action", "Index");
break;
}
}
ExceptionLogger.LogException(ex); // <- This is working. Errors get logged
routeData.Values.Add("error", ex);
Server.ClearError();
IController controller = new ErrorController();
// The next line doesn't seem to be working
controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}
的Application_Error絕對是射擊,因爲日誌記錄工作正常,但不是我的顯示自定義錯誤頁,我得到了去爸爸通用的。從博客文章的標題中可以看出,上面的代碼是從我看來的,我注意到它使用MVC框架的Release Candidate 2。在1.0中有什麼改變,導致代碼的最後一行無效?像往常一樣works great on my machine。
任何建議將不勝感激。
編輯:忘了提及我已經嘗試了Web.config中的customErrors模式的所有3種可能性(Off,On和RemoteOnly)。無論此設置如何,結果都是相同
編輯2:我以前也有和無控制器類下,[HandleError]裝修試了一下。
更新:我已經想通了,並固定在404。 Go Daddy的託管控制中心中的設置面板的一部分可以控制404行爲,默認情況下顯示其通用頁面,顯然這會覆蓋任何Web.config設置。所以我的自定義404頁面現在按預期顯示。然而,500和503仍然不起作用。我有代碼HomeController的搶內容的靜態文本版本如果SQL Server拋出異常如下:
public ActionResult Index()
{
CcmDataClassesDataContext dc = new CcmDataClassesDataContext();
// This might generate an exception which will be handled in the OnException override
HomeContent hc = dc.HomeContents.GetCurrentContent();
ViewData["bodyId"] = "home";
return View(hc);
}
protected override void OnException(ExceptionContext filterContext)
{
// Only concerned here with SqlExceptions so an HTTP 503 message can
// be displayed in the Home View. All others will bubble up to the
// Global.asax.cs and be handled/logged there.
System.Data.SqlClient.SqlException sqlEx =
filterContext.Exception as System.Data.SqlClient.SqlException;
if (sqlEx != null)
{
try
{
ExceptionLogger.LogException(sqlEx);
}
catch
{
// couldn't log exception, continue without crashing
}
ViewData["bodyId"] = "home";
filterContext.ExceptionHandled = true;
HomeContent hc = ContentHelper.GetStaticContent();
if (hc == null)
{
// Couldn't get static content. Display friendly message on Home View.
Response.StatusCode = 503;
this.View("ContentError").ExecuteResult(this.ControllerContext);
}
else
{
// Pass the static content to the regular Home View
this.View("Index", hc).ExecuteResult(this.ControllerContext);
}
}
}
下面是試圖獲取靜態內容的代碼:
public static HomeContent GetStaticContent()
{
HomeContent hc;
try
{
string path = Configuration.CcmConfigSection.Config.Content.PathToStaticContent;
string fileText = File.ReadAllText(path);
string regex = @"^[^#]([^\r\n]*)";
MatchCollection matches = Regex.Matches(fileText, regex, RegexOptions.Multiline);
hc = new HomeContent
{
ID = Convert.ToInt32(matches[0].Value),
Title = matches[1].Value,
DateAdded = DateTime.Parse(matches[2].Value),
Body = matches[3].Value,
IsCurrent = true
};
}
catch (Exception ex)
{
try
{
ExceptionLogger.LogException(ex);
}
catch
{
// couldn't log exception, continue without crashing
}
hc = null;
}
return hc;
}
我已驗證,如果更改連接字符串以生成SqlException,代碼會正確記錄該錯誤,然後獲取並顯示靜態內容。但是,如果我還將Web.config中的靜態文本文件的路徑更改爲測試Home View的503版本,我所取而代之的是僅包含「service unavailable」的頁面。而已。沒有自定義503消息與網站的外觀和感覺。
沒有人有改進的代碼,可以幫助有什麼建議?是否有助於向HttpResponse添加不同的頭文件?還是Go Daddy強行劫持503s?
+1謝謝,布萊恩。經過2個半小時的搜索,你給了我我需要的答案。它非常簡單。我試圖設置一個404錯誤代碼,它給了我一個安全異常和「Response.TrySkipIisCustomErrors = true;」修復。 – 2009-12-31 19:48:48
這讓我瘋狂。非常感謝。放置「Response.TrySkipIisCustomErrors = true;」在我的錯誤控制器中設置響應代碼後立即對我有效。這是誤導,因爲我的本地IIS 7實例呈現我的自定義錯誤視圖,但遠程登臺服務器不是。 = P – NovaJoe 2010-09-20 22:33:35
應用此答案中提供的內容後,我在同一條船上:http://stackoverflow.com/a/7499406/114029。 'Response.TrySkipIisCustomErrors = true;'來自天堂! :) – 2012-07-31 21:13:17