使用HttpHanlder处理404:File not found的问题

2014-01-28  来源:本站原创  分类:C#教程  人气:2 

本篇文章小编为大家介绍。使用HttpHanlder处理404:File not found的问题。需要的朋友参考下

一:场景

出于某种理由,需要由asp.net引擎处理所有可能出现的无效URL的Request。

二:现状

对于无效URL,一般会由IIS返回404错误。当然,也可以自定义Custom Error Page,但是在这种情况下,并不能处理原URL所代表的Request的数据(如表单数据)。或许我们想直接在Application_BeginRequest中处理该请求,但是,如果我们的IIS已经处理了“Verify that file Exists”,则实际上该请求根本不会被Application_BeginRequest命中(Fire)。

三:解决方案

1:准备好web.config,进行如下配置:

<location path="FolderTest">
  <system.web>
    <httpHandlers>
      <add verb="*" path="*" type="YourNamespace.YourHandler"/>
    </httpHandlers>
  </system.web>
  <system.webServer>
    <handlers>
      <add name="testyou" verb="*" path="*" type="younamespace.youHandler" resourceType="Unspecified"/>
    </handlers>
  </system.webServer>
</location>

有几点需要说明:

1)熟悉system.web和system.webServer的朋友一定知道,后者是针对iis7及以上的集成模式的配置项,如果不满足此宿主条件,则IIS会自动忽略此配置,采用前者的配置节点。

2)经过此配置后,iis7及以上的集成模式的站点已经能够处理FolderTest路径下的所有404 Request了。

3)resourceType="Unspecified",是用来处理标示处理所有请求的,包括file and folder等等,如果我们处理的是文件url,则不需要此配置属性。

4)path=”*”,表明这是一个wildcard httphandler。

5)如果我们不是要处理子目录下所有请求,如上文配置中的FolderTest,而是要处理根目录下的所有404请求,则去掉location配置节。

2:现在来处理iis7及以上的集成模式的情况

比如,不幸我们还在使用II6,则应该在IIS中配置“Verify that file Exists”为非构选状态。以iis6为例,我们可进行如下操作:

1:)打开站点配置节点

使用HttpHanlder处理404:File not found的问题

点击Configuration之。

2:)然后,在出现的界面中的下半部分,即“Wildcard application maps”部分,点击“Insert”,出现如下图:

使用HttpHanlder处理404:File not found的问题

把aspnet_isapi.dll配置进去,然后不要钩选“Verify that file exists”,OK之。

至此,整个世界清静了。你可以在你的HttpHandler中处理任何想要的Request的数据了,比如表单数据,哪怕客户端提交的是一个不存在的URL。

相关文章
  • 使用HttpHanlder处理404:File not found的问题 2014-01-28

    本篇文章小编为大家介绍.使用HttpHanlder处理404:File not found的问题.需要的朋友参考下 一:场景 出于某种理由,需要由asp.net引擎处理所有可能出现的无效URL的Request. 二:现状 对于无效URL,一般会由IIS返回404错误.当然,也可以自定义Custom Error Page,但是在这种情况下,并不能处理原URL所代表的Request的数据(如表单数据).或许我们想直接在Application_BeginRequest中处理该请求,但是,如果我们的II

  • Java file download in several ways 2008-05-15

    Java file download in several ways public HttpServletResponse download(String path, HttpServletResponse response) { try { // path Refers to download the file path . File file = new File(path); // Get the file name . String filename = file.getName();

  • PHP File Download Notice 2010-05-04

    <? Php / * In order to facilitate everyone, I put my problems are summarized below. Perhaps you will encounter other problems, it should be resolved according to the actual situation, but sum it up: to develop good programming habits and styles and m

  • java file upload and download 2010-08-10

    File upload is very common in web applications , Environment to achieve jsp file upload is very easy , Because there are many online file upload using java components developed , In this paper, an example commons-fileupload component , Application to

  • In WebSphere6.1 call struts2 of action under the wrong solution return 404 2010-08-11

    The 404 xxx not found, is WAS6.1 a bug. The solution is: 1, the first WAS6.1 to upgrade to 6.1.0.3 or above. 2, the WAS Console -> Application Servers -> <server> -> Web Container Settings -> Web Container -> Custom Properties, the ad

  • Summary of the browser status code (404,200, etc.) 2010-11-16

    These status codes indicate a provisional response. Response to the client prior to receiving regular, should be prepared to receive one or more 1xx responses. 100 - continue. 101 - Switching protocols. 2xx - Success Such a status code that the serve

  • 如何创建个性化404错误页面 2014-05-27

    本教程将教你如何为你的WordPress blog创建一个404页面.如果你的博客已经有这个页面,你也可以学习如何修改404页面使其变得更加友好和个性化. 由于404错误页面的特殊,很多时候人们都会忽略了它,并没有想过设计它.其实404错误页面可能意味着用户的离开或者继续浏览你的网页,如果你的404页面太过单调枯燥,游客就很可能不愿意在浏览下去:但如果你的404错误页面富有个性化和友好,就很可能会吸引游客到你的网页的其他页面浏览. 什么是404错误页面 大多数时候,您建立网站并多次检查以确保每一

  • Ajax and the XMLHttpRequest object 2009-03-13

    XMLHttpRequest object components are XMLHttp through this object, Ajax can be the same as desktop applications with the server only the exchange of data layers without having to refresh every time interface, the data do not have to deal with each of

  • Acquaintance Ajax 2009-04-18

    Ajax is a web site to build an integrated use of JavaScript and XML technologies; Ajax name: Asynchronous JavaScript and XML; The difference between Ajax and HTML: HTML is a more traditional means of transmission (Synchronous Transmission: The client

  • Rais_Writing_Index translator: Ragnarok @ Ruby Chinese community translation team 2009-10-03

    Translator: Ragnarok @ Ruby Chinese community translation team Reprint requests to retain the copyright information, rights reserved! writing / index.html Use Spec:: Ralis the preparation of sample introduction Spec:: Rails provides four kinds of dif

  • Ajax Status and readStatus 2009-11-07

    This article is reproduced in: http://aaron-ch.javaeye.com/blog/131202 Ajax Status and readStatus readyState property, including five possible values: 0: (uninitialized) send method has not been called 1: (add-in) has been called the send method, the

  • tomcat analysis of a simple web server (map) 2010-03-29

    Preceded by javaeyer recommended a book "how tomcat works", this evening looked really good, I saw was fascinated. Ever since school book example of a knocked to learn a simple web server, probably realize, of course, this simply can not call it

  • [] Tomcat analysis of a simple web server 2010-03-29

    gogole_09 wrote Preceded by javaeyer recommended a book "how tomcat works", this evening looked really good, I saw was fascinated. Ever since school book example of a knocked to learn a simple web server, probably realize, of course, this simply

  • [How Tomcat Works] Chapter 1 a simple Web server 2010-04-21

    Translator jarfield Blog http://jarfield.javaeye.co m Hypertext Transfer Protocol (HTTP) HTTP Request HTTP Response Socket Class ServerSocket Class Application HttpServer class Request Class Response Class Run the application Summary This chapter exp

  • HTTP error codes and the meaning of 2009-07-11

    2xx success 200 normal; the request has been completed. 201 normal; immediately POST command. 202 normal; has been accepted for processing, but the handling is not complete. 203 normal; partial information - the returned information is only a part of

  • [How Tomcat Works] Chapter 2, a simple Servlet Container 2008-10-10

    Translator jarfield Blog http://jarfield.javaeye.com Overview javax.servlet.Servlet Interface First application HttpServer1 class Request Class Response Class StaticResourceProcessor class ServletProcessor1 class Running Applications The second appli

  • HTTP error code and meaning 2010-03-12

    Successful 2xx 200 normal; request has been completed. 201 normal; immediately POST command. 202 normal; has been accepted for processing, but the deal has not been completed. 203 normal; some information - information that is only part of the return

  • sql injection can see iis log 2010-03-02

    1. Log file location: c: \ \ windows \ system32 \ LogFiles date that record date for the visit; time access time; s-sitename that you said on behalf of the virtual host. s-ip visitor IP; cs-method indicated that access methods, common either to GET,

  • List of HTTP status codes 2010-03-17

    English: 100: Continue 101: Switching Protocols 102: Processing 200: OK 201: Created 202: Accepted 203: Non-Authoriative Information 204: No Content 205: Reset Content 206: Partial Content 207: Multi-Status 300: Multiple Choices 301: Moved Permanentl

  • ajax + jsp example entry 2010-03-29

    Consider the following examples you will find really good simple ajax First, prepare two jsp pages 1.index.jsp code is as follows (direct copy, tested by a) <%@ page language="java" contentType="text/html; charset=utf-8" import=&quo