Custom 404 page with asp.net C # 3.5 on IIS6

for error handling I have a few lines of code to detect every error in global.asax: the void Application_Error(object sender, EventArgs e)contents of the function are as follows:

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's the thing: when I go to blabla.aspxthat which does not exist, it ends in a line shop.BLL.Utility.Errorlog.WriteError(hex, "Global.asax caught an HttpException code: " + hex.ErrorCode);and the value of the error code-2147467259

Why is it not 404?

+3
source share
4 answers

I think you should check with the method GetHttpCode().

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

MSDN:

ErrorCode HRESULT . ( ExternalException).

GetHttpCode() HTTP .

HRESULT 0x80004005 Generic Error.

+1

, ErrorCode - . GetHttpCode() HttpException. 404, .

if (hex.GetHttpCode() == 404)
+1

Source: https://habr.com/ru/post/1754398/


All Articles