首页 新闻 会员 周边 捐助

IIS7.5中集成模式的customErrors设置问题

0
悬赏园豆:5 [已关闭问题] 关闭于 2013-12-04 18:41

网站中目前web.config配置如下

<customErrors mode="On" redirectMode="ResponseRedirect">
    <error statusCode="500" redirect="~/Error.aspx?id=500" />
    <error statusCode="404" redirect="~/Error.aspx?id=404" />
</customErrors>

当redirectMode="ResponseRewrite"时Error.aspx中

var ex = Server.GetLastError(); //返回异常对象

当redirectMode="ResponseRedirect"时Error.aspx中

var ex = Server.GetLastError(); //返回null

 

 

问题是:我需要使用redirectMode="ResponseRedirect"并且在Error.aspx中能够获取错误异常。请问这个是什么原因?

dotNetDR_的主页 dotNetDR_ | 老鸟四级 | 园豆:2078
提问于:2013-11-01 11:35
< >
分享
所有回答(1)
0

HttpContext context = HttpContext.Current;

Exception exception = context.Server.GetLastError();

全局错误:

void Application_Error(object sender, EventArgs e)
{
  //This block will execute when there any error on the page and we can fetch the current context
  HttpContext context = HttpContext.Current;
 
  //This block will fetch current excpetion
  Exception exception = context.Server.GetLastError();
 
  //This block is used for creating custom error message
  StringBuilder errorDetails = new StringBuilder();
  errorDetails.Append("<br>Incorrect URL: ");
  errorDetails.Append(context.Request.Url.AbsoluteUri);
  errorDetails.Append("<br>Source: ");
  errorDetails.Append(exception.Source);
  errorDetails.Append("<br>Message: ");
  errorDetails.Append(exception.Message);
  errorDetails.Append("<br>Stack trace: ");
  errorDetails.Append(exception.StackTrace);
 
  //This block is used for fetching the error code
  HttpException httpException = (HttpException)exception;
  int errorCode = httpException.GetHttpCode();
 
  //This block will write the custom error message on the page
  context.Response.Write(errorDetails.ToString());
 
  //This will clear the error from the server and let the page finish
  context.Server.ClearError();
}

指定页面错误:

protected override void OnError(EventArgs e)
{
   //This block will execute when there any error on the page and we can fetch the current context
   HttpContext context = HttpContext.Current;
           
   //This block will fetch current excpetion
   Exception exception = context.Server.GetLastError();
 
   //This block is used for creating custom error message
   StringBuilder errorDetails = new StringBuilder();
   errorDetails.Append("<br>Incorrect URL: ");
   errorDetails.Append(context.Request.Url.AbsoluteUri);
   errorDetails.Append("<br>Source: ");
   errorDetails.Append(exception.Source);
   errorDetails.Append("<br>Message: ");
   errorDetails.Append(exception.Message);
   errorDetails.Append("<br>Stack trace: ");
   errorDetails.Append(exception.StackTrace);
 
   //This block is used for fetching the error code
   HttpException httpException = (HttpException)exception;
   int errorCode = httpException.GetHttpCode();
 
 
   //This block will write the custom error message on the page
   context.Response.Write(errorDetails.ToString());
 
   //This will clear the error from the server and let the page finish
   context.Server.ClearError();
 
   base.OnError(e);
}
悟行 | 园豆:12559 (专家六级) | 2013-11-01 12:39

Global.asax里Application_Error能够拿到Server.GetLastError。我问的问题是在自定义的页面里怎么取出那个异常对象。

支持(0) 反对(0) dotNetDR_ | 园豆:2078 (老鸟四级) | 2013-11-01 13:26

@dotNetDR_: 上面不是有吗,重写OnError方法。

支持(0) 反对(0) 悟行 | 园豆:12559 (专家六级) | 2013-11-01 20:04
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册