404 Custom Forwarding

Hi, I am trying to redirect if the answer is 404, but it does not work as expected, can you see this problem? It still refers to a total of 404

in my Global.asax

protected void Application_BeginRequest(Object sender, EventArgs e)
{
       if (Response.Status == "404 Not Found")
        {
            string notFound = "~/custom_notfound.aspx";
            Response.Redirect(notFound);
        } 

}

UPDATE

Tried so far

(Response.Status == "404 Not Found")
(Response.Status == "404")
(Response.StatusCode == 404)
+5
source share
4 answers

You can also use the customrors section of web.config - as shown here

eg. In the system.web section

<customErrors mode="On" defaultRedirect="/custom_error.aspx">
    <error statusCode="404" redirect="/custom_notfound.aspx" />
</customErrors>
+8
source

I do not think that BeginRequest could find out about 404 errors. Try using Application_Error instead. Make sure Server.GetLastError () is an HttpException, and if so, check the status.

+3

web.config , Application_BeginRequest .

. ServerFault.

web.config, , , BeginRequest, , , . 404. .

, HttpStatusCode, HttpWebResponse. , Enum.

+2

web.config

<system.webServer>
  <httpErrors errorMode="Custom" defaultResponseMode="File" >
     <remove statusCode="404" />
     <remove statusCode="500" />
     <error statusCode="404" path="404.html" />
     <error statusCode="500" path="500.html" />
   </httpErrors>
</system.webServer>
0

All Articles