Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web.config - Custom error pages not working

Tags:

.net

asp.net

iis

I have tried the following (and also tried with the commented-out uncommented instead) but only get an error:

The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

In the web.config of a published website project:

  <system.webServer>
    <httpErrors errorMode="Custom"  existingResponse="Replace">
      <remove statusCode="404"/>
      <!--<error statusCode="404" responseMode="File" path="\Error\404.htm"/>-->
      <error statusCode="404" responseMode="ExecuteURL" path="http://example.com/Error/404.htm"/>
    </httpErrors>
  </system.webServer>

I try it by changing the url in the browser from .../default.aspx (which is fine) to .../abc.aspx.

Is this the correct way to redirect to error pages, or is there some mistake here?

EDIT

I've found that if I try http://example.com/nonExistingPage - it does redirect to the error page. But not fromhttp://example.com/Folder/nonExistingPage

EDIT 2

The problem was partially solved by specifying the path after example.com. However - the site is published to example.com/subfolder and when someone navigates to example.com/nonExistingFolder - the custom error page is not shown.

like image 866
ispiro Avatar asked May 26 '15 20:05

ispiro


People also ask

How do I redirect an error page in web config?

The <customErrors> section in Web. config has two attributes that affect what error page is shown: defaultRedirect and mode . The defaultRedirect attribute is optional. If provided, it specifies the URL of the custom error page and indicates that the custom error page should be shown instead of the Runtime Error YSOD.

How do you implement a custom error page?

Steps for Custom Error PageSet <customErrors> setting in Web. Config file of the application. Pass defaultRedirect and mode attributes in <customErrors>. If you want to set your application level exception should redirect to your custom error page, you can do this by going to global.

Which is used for configuring the custom error page to custom error code in web config file?

webServer> in web. config is used to configure IIS level errors (IIS 7+). It overrides the configuration of <customErrors> section. To display a custom error page with an appropriate error code, use the <httpErrors> section only, and do not use the <customErrors> section.


1 Answers

Try this in web.config (includes 500 error support as well):

<configuration>
    ...
    <system.web>
        ...
        <customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Error/500.htm">
            <error statusCode="404" redirect="~/Error/404.htm" />
            <error statusCode="500" redirect="~/Error/500.htm" />
        </customErrors>
        ...
    </system.web>
    ...
    <system.webServer>
        ...
        <httpErrors errorMode="Custom">
            <remove statusCode="404" />
            <error statusCode="404" path="/Error/404.htm" responseMode="ExecuteURL" prefixLanguageFilePath="" />
            <remove statusCode="500" />
            <error statusCode="500" path="/Error/500.htm" responseMode="ExecuteURL" prefixLanguageFilePath="" />
        </httpErrors>
        ...
     </system.webServer>
     ...
</configuration>

I would also recommend using .aspx pages rather than .htm so that you can ensure the proper status code is set in the response headers.

<%@ Page Language="C#" %>

<% Response.StatusCode = 404; %>

<!DOCTYPE html>
<html>
<head runat="server">
    <title>404 Not Found</title>
</head>
<body>
    404 Error
</body>
</html>
like image 106
kspearrin Avatar answered Sep 26 '22 09:09

kspearrin