Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom error pages in web.config file are not covering all url paths

In my web.config file I have specified some custom errors:

<customErrors mode="RemoteOnly" defaultRedirect="~/Error">
   <error statusCode="500" redirect="~/Error" />
   <error statusCode="404" redirect="~/NotFound" />
</customErrors>

Now, some links, such as http://mysite.com/dsflhsdff will be properly redirected to mysite.com/notfound. But some links, like https://mysite.com/videoconference/0/0/0 are handled by server itself - instead of my custom error page, I am getting IIS error page (file or dir not found). In example this link - https://scyk.pl/forums/0/0/0 will produce proper 404 error (my custom error page).

What is happening here? Do I need to set up IIS custom errors manually? If so, how can I do that?

like image 287
ojek Avatar asked Nov 03 '22 22:11

ojek


2 Answers

This is because ASP.NET never even knows that there has been a request for the .htm page. IIS will handle .htm pages by itself without involving ASP.NET at all.

You can get your custom page to show in one of two ways:

Get ASP.NET to process .htm pages: In IIS rightclick your website/virtual directory --> properties --> home directory/virtual directory tab --> "configuration" button under the "application settings" section --> Add the mapping. Set the custom error page for IIS: In IIS rightclick your website/virtual directory --> properties --> custom errors tab --> set the 404 error to the page your error page.

like image 122
Vishal Avatar answered Nov 11 '22 09:11

Vishal


You can also handle this error in your Global.asax file in Application_Error method

e.g.

  void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs
        Exception exc = Server.GetLastError();

        // Handle HTTP errors
        if (exc.GetType() == typeof(HttpException))
        {
            // The Complete Error Handling Example generates
            // some errors using URLs with "NoCatch" in them;
            // ignore these here to simulate what would happen
            // if a global.asax handler were not implemented.
            /*if (exc.Message.Contains("NoCatch") || exc.Message.Contains("maxUrlLength"))
                return;*/

            //Redirect HTTP errors to HttpError page
            /*Server.Transfer("HttpErrorPage.aspx");*/
        }
    }

More info : http://www.asp.net/web-forms/tutorials/aspnet-45/getting-started-with-aspnet-45-web-forms/aspnet-error-handling

like image 24
Motoo Avatar answered Nov 11 '22 09:11

Motoo