简体   繁体   中英

web.config not forwarding to 404 error page on non .aspx pages

Objective

I want all URLs of missing pages to forward to my 404 page which is in my root as 404error.aspx

Problem

So far only the URLs that have a .aspx will work. For example, if you enter 4error.aspx you will be redirected to the error page.

/404error.aspx?aspxerrorpath=/4error.aspx

Background

I am not a .NET developer, so I just took over this project which uses classic .aspx . So I am not using any Microsoft product to build any of this in templates or frameworks. I am just coding in Sublime text.

What I already researched

I view many articles that I found on Google but nothing had a full example that actually worked.

Code

This is the entire code that I started with in my web.config

web.config

<configuration>
  <system.web>
    <customErrors mode="RemoteOnly" defaultRedirect="~/404error.aspx" />
      <globalization
        fileEncoding="utf-8"
        requestEncoding="utf-8"
        responseEncoding="utf-8"
        culture="en-US"
        uiCulture="de-DE"
      />
  </system.web>
</configuration>

I have also found this example from Microsoft (404error.aspx was my modification)
http://msdn.microsoft.com/en-us/library/vstudio/bb397417%28v=vs.100%29.aspx

web.config (2)

<configuration>
  <appSettings/>
  <connectionStrings/>
  <system.web>
    <compilation debug="true" />

    <!-- Turn on Custom Errors -->
    <customErrors mode="On" 
      defaultRedirect="/404error.aspx">
      <error statusCode="404" redirect="/404Error.aspx"/>
    </customErrors>

  </system.web>
</configuration>

That also did not handle pages that did not have the .aspx

Then I tried this example from

web.config (3)

<?xml version="1.0"?>
<configuration>
   <system.web>
    <customErrors mode="RemoteOnly" defaultRedirect="~/404error.aspx" />
    <error statusCode="404" redirect="~/404error.aspx" />
    <globalization
      fileEncoding="utf-8"
      requestEncoding="utf-8"
      responseEncoding="utf-8"
      culture="en-US"
      uiCulture="de-DE"
    />
   </system.web>
   <system.webServer>
      <httpErrors>
        <remove statusCode="401" subStatusCode="-1" />
        <remove statusCode="403" subStatusCode="-1" />      
        <remove statusCode="404" subStatusCode="-1" />                
        <remove statusCode="500" subStatusCode="-1" />
          <!-- full url when responsemode is Redirect -->
        <error statusCode="401" path="http://foo.com/default.htm" responseMode="Redirect" />
          <!-- local relative path when responsemode is ExecuteURL -->
        <error statusCode="403" path="~/404error.aspx" responseMode="ExecuteURL" />
        <error statusCode="404" path="~/404error.aspx" responseMode="ExecuteURL" />                
        <error statusCode="500" path="~/404error.aspx" responseMode="ExecuteURL" />
      </httpErrors>
      <modules runAllManagedModulesForAllRequests="true"/>
   </system.webServer>
</configuration>

Obviously I have to change the paths for other-than-404 pages but I just wanted to test it for those errors. That last web.config works even worse.

Can you please tell me what I need to modify. I would appreciate a full <configuration> because I keep getting confused what goes where.

EDIT 1

I had read that many developers were suggesting to just make it a HTML page. So now my page is 404.html Here is my updated web.config

<configuration>
  <system.web>
    <customErrors mode="On"
              redirectMode="ResponseRewrite">
        <error statusCode="404"
           redirect="~/404.html"/>
      </customErrors>
      <globalization
        fileEncoding="utf-8"
        requestEncoding="utf-8"
        responseEncoding="utf-8"
        culture="en-US"
        uiCulture="de-DE"
      />
  </system.web>

  <system.webServer>
    <httpErrors errorMode="Custom"
            defaultResponseMode="File">
      <remove statusCode="404"/>
      <error statusCode="404"
          path="~/404.html"/>
    </httpErrors>
  </system.webServer>
</configuration>

You need to configure the <httpErrors> element. This configures the error pages for both static files and server pages.

Your 3rd attempt "web.config (3)" and "Edit 1" are almost there. The problem is that you can't use app-relative paths here (ex: "~/404.html"), they have to be relative from the site root (ex: "/404.html").

<?xml version="1.0"?>
<configuration>
   <system.webServer>
      <httpErrors>
        <remove statusCode="401" subStatusCode="-1" />
        <remove statusCode="403" subStatusCode="-1" />      
        <remove statusCode="404" subStatusCode="-1" />                
        <remove statusCode="500" subStatusCode="-1" />
          <!-- full url when responsemode is Redirect -->
        <error statusCode="401" path="http://foo.com/default.htm" responseMode="Redirect" />
          <!-- local relative path when responsemode is ExecuteURL -->
        <error statusCode="403" path="/404error.aspx" responseMode="ExecuteURL" />
        <error statusCode="404" path="/404error.aspx" responseMode="ExecuteURL" />                
        <error statusCode="500" path="/404error.aspx" responseMode="ExecuteURL" />
      </httpErrors>
   </system.webServer>
</configuration>

The settings in the example customErrors section cause any unhandled HTTP 404 (file not found) errors to be directed to the Http404ErrorPage.aspx file. These HTTP 404 errors would occur if a request were made for an .aspx file, .asmx file, and so on and if the requested file did not exist. All other unhandled errors in ASP.NET files are directed to the DefaultRedirectErrorPage.aspx file.

<configuration>
  <appSettings/>
  <connectionStrings/>
  <system.web>
    <compilation debug="true" />

    <!-- Turn on Custom Errors -->
    <customErrors mode="On" 
      defaultRedirect="DefaultRedirectErrorPage.aspx">
      <error statusCode="404" redirect="Http404ErrorPage.aspx"/>
    </customErrors>

  </system.web>
</configuration>

NoteNote: In the example, the mode attribute is set to "On" so that you can error messages when you run the example in Visual Studio. In a production environment, this setting would normally be "RemoteOnly". ASP.NET then renders error pages to external users. If a request is made on the server computer (localhost), ASP.NET renders a page with detailed error information.

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