SoFunction
Updated on 2025-04-06

Using HttpHanlder to handle the issue of 404:File not found

1: Scene

For some reason, the engine needs to handle all possible invalid URL requests.

Two: Current situation

For invalid URLs, IIS will generally return a 404 error. Of course, you can also customize the Custom Error Page, but in this case, the data of the Request represented by the original URL (such as form data) cannot be processed. Maybe we want to handle the request directly in the Application_BeginRequest, but if our IIS has already processed "Verify that file Exists", the request will not actually be hit by the Application_BeginRequest at all.

Three: Solution

1: Be prepared and make the following configuration:

Copy the codeThe code is as follows:

<location path="FolderTest">
  <>
    <httpHandlers>
      <add verb="*" path="*" type=""/>
    </httpHandlers>
  </>
  <>
    <handlers>
      <add name="testyou" verb="*" path="*" type="" resourceType="Unspecified"/>
    </handlers>
  </>
</location>

There are a few points to be explained:

1) Friends who are familiar with it must know that the latter is a configuration item for the integration mode of iis7 and above. If this host condition is not met, IIS will automatically ignore this configuration and use the former configuration node.

2) After this configuration, sites in iis7 and above integrated mode can already handle all 404 Requests under the FolderTest path.

3) resourceType="Unspecified" is used to handle all requests, including file and folder, etc. If we are processing file url, this configuration property is not required.

4) path=”*”, indicating that this is a wildcard httphandler.

5) If we do not want to process all requests in the subdirectory, such as FolderTest in the above configuration, but to process all 404 requests in the root directory, then remove the location configuration section.

2: Now deal with the integration mode of iis7 and above

For example, unfortunately we are still using II6, we should configure "Verify that file Exists" to be non-selected in IIS. Taking iis6 as an example, we can perform the following operations:

1:) Open the site configuration node

Click Configuration.

2:) Then, in the lower half of the interface that appears, that is, the "Wildcard application maps" section, click "Insert", and the following figure appears:

Configure aspnet_isapi.dll, and then do not select "Verify that file exists", OK.

At this point, the whole world has become quiet. You can process any desired Request data in your HttpHandler, such as form data, even if the client submits a non-existent URL.