ASP.Net 404 Redirect for friendly url not working

2019-07-29 07:16发布

问题:

I have the below code to do 404 redirect in Global.asax.cs

   protected void Application_Error(object sender, EventArgs e)
    {
        Exception ex = Server.GetLastError();
        if (ex.InnerException != null)
            ex = ex.InnerException;
        if ((ex is HttpException && ((HttpException)ex).GetHttpCode()==404 ))
        {
            Response.Redirect("~/Custom404Page.aspx");
        }
    }

It works fine in visual studio and getting user friendly Custom404Page.

But when I deploy the same in IIS 7.0, it is not at all working. Shows the below message

In web.config

<customErrors mode="RemoteOnly" defaultRedirect="/Error.aspx" />

Due to some reason we are asked not to put any entries in web.config for statusCode="404". So we did in global.asax.cs

Why it is not working in IIS.

UPDATE: domain/blahblah is not showing Custom404Page. But domain.blahblah.aspx, shows Custom404Page.aspx. How to make it work with SEO friendly url domain/blahblah