+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / Handling all 404s through ASP.Net

Handling all 404s through ASP.Net

5 posts, 0 answered
  1. Joe
    Joe avatar
    16 posts
    Registered:
    10 May 2010
    18 Feb 2011
    Link to this post
    Hi,

    I need someone to point me in the right direction here.

    I currently have a site with the following setup:

    IIS 6 and .Net 2.0
    I have AdvancedUrlRewriting enabled.
    I have extensionless urls, so I had to add a wildcard mapping with "Verify file exists" unchecked.

    I would like to handle any 404 in a standard way.

    I tried adding the following code to the Application_Error in Global.asax:

    HttpException serverError = (HttpException)Server.GetLastError();
     
    if (serverError != null) {
        int errorCode = serverError.GetHttpCode();
        if (errorCode == 404) {
            Server.ClearError();
            Server.Transfer("/errorPage.aspx");
        }
    }

    But this is only being handled for aspx files. Any request to a non existent file or page that doesn't have an aspx extension is getting the standard IIS error.

    Is there a way that I can handle all 404s through ASP.Net?  I also need to keep the url entered into the browser, and not redirect to an error page, hence the Server.Transfer.
  2. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    22 Feb 2011
    Link to this post
    Hello Massimiliano,

    Thank you for contacting Telerik Support.

    You can try using the ASP.NET customErrors element - you should get 404 code regardless of the page extension. Your approach though can work only if you have wildcard mapping, so all requests, not only the aspx ones go through the Asp.Net ISAPI.

    Best wishes,
    Georgi
    the Telerik team
  3. Joe
    Joe avatar
    16 posts
    Registered:
    10 May 2010
    22 Feb 2011
    Link to this post
    Don't customErrors redirect to the error page provided?

    I am on .Net 2.0 so I cannot use responserewrite
  4. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    28 Feb 2011
    Link to this post
    Hi Massimiliano Rizzuto,

    Yes those errors redirect to the error page. However as Ivan has mentioned you need a wildcard handler mapping to aspnet_isapi so that the 404 error is handled by the website instead of IIS returning a 404.

    Best wishes,
    Radoslav Georgiev
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  5. Joe
    Joe avatar
    16 posts
    Registered:
    10 May 2010
    10 Mar 2011
    Link to this post
    Hi, apologies for the late reply.

    I've already mentioned that I have a wildcard mapping to as aspnet_isapi.
Register for webinar
5 posts, 0 answered