我在与上一个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