C# 发布到IIS时不再触发应用程序错误
好的,我的Global.asax文件中有以下代码:C# 发布到IIS时不再触发应用程序错误,c#,asp.net,iis-7.5,C#,Asp.net,Iis 7.5,好的,我的Global.asax文件中有以下代码: void Application_Error(object sender, EventArgs e) { // Code that runs when an unhandled error occurs Exception objError = Server.GetLastError().GetBaseException(); Response.Redirect( String.
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
Exception objError = Server.GetLastError().GetBaseException();
Response.Redirect(
String.Format(
"/Error/{0}/{1}",
((HttpException)objError).GetHttpCode(),
Request.RawUrl));
}
提供整洁的错误URL,如“/error/404/theNameofRequestedPage”。这在VS 2008中运行良好,但一旦发布到我的本地计算机上,我会得到默认错误页面:
错误摘要
HTTP错误404.0-找不到
您正在寻找的资源已被删除
已被删除,名称已更改,或
暂时不可用
有人知道怎么做吗?我选择不使用system.web/customErrors,因为我无法从那里访问Server.GetLastError()(或者至少它从未对我起作用),我想获取http代码。这很可能与您触发IIS http错误有关,该错误在节点下的web.config中定义
<system.webServer>
<httpErrors>
</httpErrors>
<system.webServer>
在您让响应完成之前,否则IIS将拦截错误
这是完全不直观的,尤其是如果您自己设置状态代码。我试图找到一种在Microsoft Connect上提交错误的方法,即手动设置http错误代码不会自动设置
tryskipiiscustomerror
,但似乎无法找到任何要提交的相关产品。我遇到了类似的问题,并调用了服务器。ClearError()
在重定向解决问题之前
如果是你,我会写信给你
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
Exception objError = Server.GetLastError();
if(objError is HttpException){
//Need to clear the error, otherwise the buil-in redirect would occure
Server.ClearError();
Response.Redirect(
String.Format(
"/Error/{0}/{1}",
((HttpException)objError).GetHttpCode(),
Request.RawUrl));
}
}
请注意,Server.GetLastError().GetBaseException()返回base异常,它并不总是HttpException,您要查找的只是GetLastError()
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
Exception objError = Server.GetLastError();
if(objError is HttpException){
//Need to clear the error, otherwise the buil-in redirect would occure
Server.ClearError();
Response.Redirect(
String.Format(
"/Error/{0}/{1}",
((HttpException)objError).GetHttpCode(),
Request.RawUrl));
}
}