locked
Custom errors section does not redirect 404 errors RRS feed

  • Question

  • User-13613444 posted

    Hi,

    I managed to secure all applications on my server with the custom errors script section mentioned in Scott Gu's blog post. All but one website...

    That particular website redirects errors for all error codes to the same page, except 404 errors. For 404 errors it shows a standard 404 error page that seems to come with IIS 7.

    The custom errors section looks like this:

    <customErrors mode="On" defaultRedirect="/Error/ServerError/500" />		


    Other sites on the same server do not have this problem. Please advise.



    Saturday, September 18, 2010 10:09 AM

Answers

  • User-158764254 posted

    No, the App Pool is running in Integrated mode.


    Still haven't found any way around this... please help!

     

    Do you have something in your error page page that is accessing Session?

    Or is your error page using a masterpage or other control that might access Session?

    While testing,

    • If i had a defaultRedirect error page access Session
    • and if the customErrors was set to use redirectMode="ResponseRewrite"
    • then upon accessing a missing page, i got the standard 404 error instead on my specified error page.

    As soon as i removed any Session access, my custom error page appeared.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 20, 2010 7:12 PM

All replies

  • User753188975 posted

    Hi

    IIS 7 has it's own error handling apart from .NET's error handling. The configuration for that is located under 


    <httpErrors defaultResponseMode="ExecuteURL" defaultPath="~/error.aspx">
     <clear/>
     <error statusCode="404" responseMode="ExecuteURL" path="/Page-not-found/"/>
     </httpEr
    <system.webserver>
    <httpErrors errorMode="Custom" defaultResponseMode="ExecuteURL" defaultPath="~/error.aspx">
    		  <clear/>
    		  <error statusCode="404" responseMode="ExecuteURL" path="~/error.aspx/"/>
    	  </httpErrors>
    </system.webServer>


    If I read it correctly then that should also redirect to the same error page that you are using in the <customErrors> section. Then your config file should look like this:


    <system.webserver>
    <httpErrors errorMode="Custom" defaultResponseMode="ExecuteURL" defaultPath="~/error.aspx">
    		  <clear/>
    	  </httpErrors>
    </system.webServer>

    By adding the <clear/> tag you override all default configuration and your error page should redirect correctly.


    Please correct me if I'm wrong




    Saturday, September 18, 2010 10:56 AM
  • User-13613444 posted

    Changing or removing the httpErrors section seems to alter the error page. But now I just get a different GENERIC error page and still not the page I actually need to display. 

    For testing purposes I have replaced the httpErrors section with this:


     <httpErrors errorMode="Custom" defaultResponseMode="Redirect" defaultPath="~/Error/ServerError">
     <clear/>
     </httpErrors>

     <httpErrors errorMode="Custom" defaultResponseMode="Redirect" defaultPath="~/Error/ServerError">

     <clear/>

     </httpErrors>

    There is no redirection though. Just a generic error page saying "The page cannot be displayed because an internal server error has occurred.". I made sure that /Error/ServerError works fine, so I don't think this could be the problem.

    Please help!



    Sunday, September 19, 2010 3:49 AM
  • User-158764254 posted

    Is that app by any chance set up for the 1.1 framework?

    If so you may be seeing an error with the TryCast code in the error page.

    If you have access to log on to that server, Try setting your error mode to LocalOnly and then log on to the server and request a non existent page.  Instead of a generic page, you should get the real error page so you can see what's wrong.

     

    Sunday, September 19, 2010 8:01 AM
  • User-13613444 posted

    Hi Mike,

    The site is running .NET 4.0

    There's nothing to see with LocalErrors. My problem is that I get a generic 404 page, not an internal server error (or the custom error page that should actually appear).

    Sunday, September 19, 2010 10:19 AM
  • User-1635603906 posted

    Is your AppPool in Classic mode? If so, then the script map will not utilize the ASP.NET for non-.NET file types?

    Monday, September 20, 2010 6:18 AM
  • User-13613444 posted

    No, the App Pool is running in Integrated mode.


    Still haven't found any way around this... please help!

    Monday, September 20, 2010 3:17 PM
  • User-234406897 posted

    I think you will need to go to an actual page rather than a path

    <customErrors mode="On" defaultRedirect="/Error/ServerError/500" /> 

    <httpErrors errorMode="Custom" defaultResponseMode="Redirect" defaultPath="~/Error/ServerError">

    You have been trying to go to somewhere with and extension. Try something like:

    <customErrors mode="On" defaultRedirect="/Error/ServerError/500.aspx" /> 

    If not try going to a basic htm page first and then to aspx pages.

    Monday, September 20, 2010 3:28 PM
  • User-158764254 posted

    No, the App Pool is running in Integrated mode.


    Still haven't found any way around this... please help!

     

    Do you have something in your error page page that is accessing Session?

    Or is your error page using a masterpage or other control that might access Session?

    While testing,

    • If i had a defaultRedirect error page access Session
    • and if the customErrors was set to use redirectMode="ResponseRewrite"
    • then upon accessing a missing page, i got the standard 404 error instead on my specified error page.

    As soon as i removed any Session access, my custom error page appeared.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 20, 2010 7:12 PM
  • User-852409647 posted

    Agreed.  I had the same issue.  In VS if you do Ctrl-Alt-E and enable breaking on the managed code first chance exceptions you can see where you code is possibly using a resource not available during a 404.

    Monday, September 20, 2010 7:32 PM
  • User-408624355 posted

    Hi James,

     Even I had the same Issue, But i found solution in the below page : http://briancaos.wordpress.com/2012/06/12/custom-404-page-ignored-by-iis-7/

    If your IIS does not display your own custom error page, this might be the problem – and the solution:

    I recently had to implement a custom 404 error page. The code for that is pretty simple. In the web.config, the custom 404 error page is added:

    1 <customErrors mode="Off">
    2   <error statusCode="404" redirect="/components/Error/404.aspx" />
    3 </customErrors>

    And the page is requested to return a 404 not found:

    1 protected void Page_Load(object sender, EventArgs e)
    2 {
    3   Response.StatusCode = 404;
    4   Response.StatusDescription = "Page not found";
    5   Response.Flush();
    6 }

    Here is the problem: The code worked fine on my Windows 7 developer machine’s localhost, but when I copied the code to my Windows 2008 server, the IIS refused to call my custom page, displaying the default 404 error page instead.

    Here is the catch: The Windows 2008 server is running the latest IIS 7.5, and this version will by default “override” the HTTP response with its default error pages if the page returns a HTTP error status code (like 404, or 500).

    Here is the solution: .NET 3.5 has a new property calledResponse.TrySkipIisCustomErrors that will, if set to true, instruct IIS 7.5 to ignore it’s own default error pages, allowing me to use my own. The code change is very small, but highly effective:

    1 protected void Page_Load(object sender, EventArgs e)
    2 {
    3   // Allow me to use my own error page:
    4   Response.TrySkipIisCustomErrors = true;
    5   Response.StatusCode = 404;
    6   Response.StatusDescription = "Page not found";
    7   Response.Flush();
    8 }

    Thursday, October 11, 2012 3:35 AM
  • User-408624355 posted

    Hi James,

     Even i had the same Issue Found Solution in the below Link Please check :

    http://briancaos.wordpress.com/2012/06/12/custom-404-page-ignored-by-iis-7/

    If your IIS does not display your own custom error page, this might be the problem – and the solution:

    I recently had to implement a custom 404 error page. The code for that is pretty simple. In the web.config, the custom 404 error page is added:

    1 <customErrors mode="Off">
    2   <error statusCode="404" redirect="/components/Error/404.aspx" />
    3 </customErrors>

    And the page is requested to return a 404 not found:

    1 protected void Page_Load(object sender, EventArgs e)
    2 {
    3   Response.StatusCode = 404;
    4   Response.StatusDescription = "Page not found";
    5   Response.Flush();
    6 }

    Here is the problem: The code worked fine on my Windows 7 developer machine’s localhost, but when I copied the code to my Windows 2008 server, the IIS refused to call my custom page, displaying the default 404 error page instead.

    Here is the catch: The Windows 2008 server is running the latest IIS 7.5, and this version will by default “override” the HTTP response with its default error pages if the page returns a HTTP error status code (like 404, or 500).

    Here is the solution: .NET 3.5 has a new property calledResponse.TrySkipIisCustomErrors that will, if set to true, instruct IIS 7.5 to ignore it’s own default error pages, allowing me to use my own. The code change is very small, but highly effective:

    1 protected void Page_Load(object sender, EventArgs e)
    2 {
    3   // Allow me to use my own error page:
    4   Response.TrySkipIisCustomErrors = true;
    5   Response.StatusCode = 404;
    6   Response.StatusDescription = "Page not found";
    7   Response.Flush();
    8 }
    Thursday, October 11, 2012 3:37 AM