asax中自定义错误

asax中自定义错误

本文介绍了在Global.asax中自定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的的Global.asax 文件,我有以下的code:

In my global.asax file, I have the following code:

void Application_Error(object sender, EventArgs e)
{
    Exception TheError = Server.GetLastError();

    if (TheError is HttpException && ((HttpException)TheError).GetHttpCode() == 404)
    {
        Response.Redirect("~/404.aspx");
    }
    else
    {
        Response.Redirect("~/500.aspx");
    }
}

当我浏览到一个不存在的页面,我得到的一般错误页面。我不希望任何关于在 web.config中的自定义错误,因为我的计划是code添加到的Global.asax 文件来记录异常。有没有一种方法来处理自定义错误只在Global.asax文件?

When I navigate to an non-existing page, I get the generic error page. I don't want anything pertaining to custom error in the web.config because my plan is to add code to the global.asax file to log the exceptions. Is there a way to handle custom error with just the global.asax file?

感谢。

推荐答案

答案是:不可以明显,但是这似乎可以解释它:http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/displaying-a-custom-error-page-cs

The answer is not obvious, but this seems to explain it: http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/displaying-a-custom-error-page-cs

如果您向下滚动的方式,你会发现这个可爱的珍闻:

If you scroll down a ways, you'll find this lovely tidbit:

注意:当一个请求时自定义错误页只显示
  由ASP.NET引擎处理的资源。
正如我们在讨论
  IIS和AS​​P.NET开发服务器之间的差异核心
  教程,web服务器可以处理特定请求本身。通过
  默认情况下,IIS Web服务器处理静态内容的请求像
  图像和HTML文件,而不必调用ASP.NET引擎。
  因此,如果用户请求不存在的图像文件他们将
  回到IIS默认的404错误消息,而不是ASP.NET的
  配置错误页面。

我已经强调了第一线。我在默认模板测试了这一点,果然,这个网址:

I've emphasized the first line. I tested this out in the default template, and sure enough, this URL:

的http://本地主机:49320 / fkljflkfjelk

让你的IIS默认页面,而只需追加的.aspx使得的Application_Error 踢。所以,这听起来像你需要启用的customErrors / httpErrors如果你想有处理的所有错误。

gets you the default IIS page, whereas simply appending a .aspx makes the Application_Error kick in. So, it sounds like you need to enable customErrors/httpErrors if you want to have all errors handled.

对于IIS< = 6 添加到<&的System.Web GT;

<customErrors mode="On" redirectMode="ResponseRewrite">
  <error statusCode="404" redirect="/404.aspx"/>
  <error statusCode="500" redirect="/500.aspx"/>
</customErrors>

对于IIS7 + 后,添加到&LT; system.webServer&GT;

<httpErrors errorMode="Custom">
  <remove statusCode="404"/>
  <error statusCode="404" path="/404.aspx" responseMode="ExecuteURL"/>
  <remove statusCode="500"/>
  <error statusCode="500" path="/500.aspx" responseMode="ExecuteURL"/>
</httpErrors>

万一有人发现它有用,我会离开原来的答案。

I'll leave the original answer in case someone finds it useful.

我相信你需要清除响应中现有的错误code,否则IIS忽略您的重定向有利于处理错误。还有一个布尔型标志, TrySkipIisCustomErrors ,可以为IIS的较新版本设置(7 +)。

I believe you need to clear the existing error code from the response, otherwise IIS ignores your redirect in favor of handling the error. There's also a Boolean flag, TrySkipIisCustomErrors, you can set for newer versions of IIS (7+).

所以,这样的事情:

void Application_Error(object sender, EventArgs e)
{
    Exception TheError = Server.GetLastError();
    Server.ClearError();

    // Avoid IIS7 getting in the middle
    Response.TrySkipIisCustomErrors = true;

    if (TheError is HttpException && ((HttpException)TheError).GetHttpCode() == 404)
    {
        Response.Redirect("~/404.aspx");
    }
    else
    {
        Response.Redirect("~/500.aspx");
    }
}

这篇关于在Global.asax中自定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:24