asp.net MVC3上的自定義錯誤頁面我正在開發(fā)一個MVC3基礎網站,我正在尋找一個處理錯誤的解決方案,并為每種錯誤渲染自定義視圖。因此,假設我有一個“錯誤”控制器,其主要操作是“索引”(通用錯誤頁面),并且此控制器將針對用戶可能出現(xiàn)的錯誤(如“Handle500”或“HandleActionNotFound”)執(zhí)行更多操作。因此,網站上可能發(fā)生的每個錯誤都可能由此“錯誤”控制器處理(例如:“Controller”或“Action”未找到,500,404,dbException等)。我使用Sitemap文件來定義網站路徑(而不是路由)。這個問題已經回答了,這是對Gweebz的回復我的最終applicaiton_error方法如下:protected void Application_Error() {//while my project is running in debug modeif (HttpContext.Current.IsDebuggingEnabled && WebConfigurationManager.AppSettings["EnableCustomErrorPage"].Equals("false")){
Log.Logger.Error("unhandled exception: ", Server.GetLastError());}else{
try
{
var exception = Server.GetLastError();
Log.Logger.Error("unhandled exception: ", exception);
Response.Clear();
Server.ClearError();
var routeData = new RouteData();
routeData.Values["controller"] = "Errors";
routeData.Values["action"] = "General";
routeData.Values["exception"] = exception;
IController errorsController = new ErrorsController();
var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
errorsController.Execute(rc);
}
catch (Exception e)
{
//if Error controller failed for same reason, we will display static HTML error page
Log.Logger.Fatal("failed to display error page, fallback to HTML error: ", e);
Response.TransmitFile("~/error.html");
}}}
asp.net MVC3上的自定義錯誤頁面
慕尼黑的夜晚無繁華
2019-08-09 10:52:34