我爲我的應用程序定義了自定義錯誤處理,它的所有工作都非常出色 - 當找不到資源時,正確的「NotFound」視圖呈現。當發生不受歡迎的異常時,'ServerError'視圖呈現。MVC3錯誤處理 - 堅持使用稱爲'錯誤'的視圖
我面對的問題是,我的應用程序堅持嘗試找到一個名爲'錯誤'的視圖,但沒有找到它,因爲我沒有一個,因此這個異常在我的自定義錯誤處理例程期間被拋出:
「視圖‘錯誤’或它的主人沒有被發現或沒有視圖引擎支持的搜索位置以下地點進行了全面搜查。......」
我有一個Application_Error事件這確實記錄所有未處理的例外:
protected void Application_Error(Object sender, EventArgs e)
{
Exception lastEx = Server.GetLastError();
// Attempt to Log the error
try
{
Utils.Logger.Error(lastEx);
}
catch (Exception loggingEx)
{
// Try and write the original ex to the Trace
Utils.TryTrace(lastEx.Message);
// Try and write the logging exception to the Trace
Utils.TryTrace(loggingEx.Message);
}
}
我已接通的customErrors「開啓」在我的web.config:
<customErrors mode="On" defaultRedirect="blah">
<error statusCode="404" redirect="dee"/>
<error statusCode="500" redirect="blah"/>
</customErrors>
而我在我的Global.asax.cs的RegisterRoutes方法定義的路由,其對應於在web.config中定義上述重定向:
routes.MapRoute(
"Error - 404",
"dee",
new { controller = "Error", action = "NotFound" }
);
routes.MapRoute(
"ServerError", // When this route is matched we want minimal error info displayed
"blah",
new { controller = "Error", action = "ServerError" }
);
我有一個BaseController其中包含OnActionExecuted程序:
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
Logger.Info(String.Format(Constants.LOG_ACTION_EXECUTED, filterContext.Controller.ToString(), filterContext.ActionDescriptor.ActionName));
// Log any exceptions
if (filterContext.Exception != null)
{
Stack<string> messages = new Stack<string>();
Exception current = filterContext.Exception;
messages.Push(current.Message);
while (current.InnerException != null)
{
messages.Push(current.InnerException.Message);
current = current.InnerException;
}
StringBuilder result = new StringBuilder();
while (messages.Count != 0)
{
result.Append(messages.Pop());
string nextline = messages.Count > 0 ? "OUTER EXCEPTION " + messages.Count + ": " : "";
result.Append(Environment.NewLine);
}
Logger.Error(result.ToString());
}
base.OnActionExecuted(filterContext);
}
是否有其他地方的框架定義了視圖來呈現未處理的異常事件?
我的自定義錯誤處理例程錯過了最後一步,這將確保框架不再期望找到'錯誤'視圖?
Eranga:這不是問題,但讓我找到如下所述的解決方案 - 謝謝! – JTech 2011-12-19 04:15:02
謝謝!那對我來說就是這樣。對於新的模板中的一些新用戶,這將位於App_Start文件夾FilterConfig.cs中。 – 2012-07-26 18:03:51