简体   繁体   English

在IIS6上使用asp.net C#3.5自定义404页面

[英]custom 404 page with asp.net C# 3.5 on IIS6

for error handling I have a few lines of code for catching every error in the global.asax : void Application_Error(object sender, EventArgs e) the content of the function looks like this: 对于错误处理我有几行代码来捕获global.asax中的每个错误: void Application_Error(object sender, EventArgs e)函数的内容如下所示:

try
    {
       Exception objErr = Server.GetLastError().GetBaseException();

       if (!(objErr is HttpException))
       {
           shop.BLL.Utility.Errorlog.WriteError(objErr, "Global.asax caught an Exception");
       }
       else
       {
           HttpException hex = (HttpException)objErr;
           if (hex.ErrorCode == 404)
               Response.Redirect("404.aspx?msg=" + hex.Message);
           else
           {
               shop.BLL.Utility.Errorlog.WriteError(hex, "Global.asax caught an HttpException code: " + hex.ErrorCode);
           }
       }

    }
    catch { }

    Server.ClearError();

now here is the thing: when I go to blabla.aspx , which does not exists, it ends up on line shop.BLL.Utility.Errorlog.WriteError(hex, "Global.asax caught an HttpException code: " + hex.ErrorCode); 现在就是这样:当我去blabla.aspx ,它不存在,它最终在线上shop.BLL.Utility.Errorlog.WriteError(hex, "Global.asax caught an HttpException code: " + hex.ErrorCode); and the value of the errorcode is -2147467259 并且错误代码的值是-2147467259

Why isn't it a 404? 为什么不是404?

I think you should check with GetHttpCode() method. 我认为你应该检查GetHttpCode()方法。

 HttpException hex = (HttpException)objErr;
 if (hex.GetHttpCode() == 404)
     Response.Redirect("404.aspx?msg=" + hex.Message);

The page not found doesn't throw an exception, the 404 error is an Http response code. 找不到页面不会抛出异常,404错误是Http响应代码。 If you are trying to set up a custom error page for 404 handling, you can set it up using the 如果您尝试为404处理设置自定义错误页面,可以使用

<customErrors>

tag in your web.config web.config中的标记

have a look at these articles... 看看这些文章......

http://aspnetresources.com/articles/CustomErrorPages and http://www.asp.net/hosting/tutorials/displaying-a-custom-error-page-cs http://aspnetresources.com/articles/CustomErrorPageshttp://www.asp.net/hosting/tutorials/displaying-a-custom-error-page-cs

Dave 戴夫

From MSDN docs : 来自MSDN文档

ErrorCode Gets the HRESULT of the error. ErrorCode获取错误的HRESULT。 (Inherited from ExternalException ). (继承自ExternalException )。

GetHttpCode() Gets the HTTP response status code to return to the client. GetHttpCode()获取返回客户端的HTTP响应状态代码。

The HRESULT 0x80004005 means Generic Error . HRESULT 0x80004005表示Generic Error

I don't think you want to use ErrorCode - that is for the internal error. 我认为您不想使用ErrorCode - 这是内部错误。 Try using GetHttpCode() on your HttpException object. 尝试在HttpException对象上使用GetHttpCode()。 That should return the 404 you are looking for. 这应该返回你正在寻找的404。

if (hex.GetHttpCode() == 404)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM