简体   繁体   中英

Web.config - Custom error pages not working

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 from http://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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM