且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

asp.net自定义HttpHandler和URL路由

更新时间:2022-12-11 14:15:15

所以您有一个实现 IHttpHandler 的类code>称为: MyHandler ,它位于名称空间 Example 中,您需要在Web.Config 。 aspx rel = nofollow> httpHandlers 部分:

So you have a class that implements IHttpHandler called: MyHandler and it's in the namespace Example, you need to make the following entries in the site's Web.Config in the httpHandlers section:

<httpHandlers>
  <add verb="*" path="*" type="Example.MyHandler"/>
</httpHandlers>

由于这会将网站/应用程序的所有URL重定向到处理程序,因此您必须考虑如何提供服务静态内容(img,脚本,样式表等)。一种方法是将此类静态内容存储在统一的URL中,例如 http://example.com/static / ... ,然后可以将处理程序设置为: / p>

Since this redirects all URLs for your web site/application to your handler you have to consider how to serve static content (imgs, scripts, style sheets etc). One way is to store such static content in a consistent URL like http://example.com/static/..., you can then set your handlers as such:

<httpHandlers>
  <add verb="*" path="*" type="Example.MyHandler"/>
  <add verb="GET,HEAD" path="static/*" type="System.Web.StaticFileHandler" />
</httpHandlers>

对于您的本地开发Web服务器(嵌入在Visual Studio中),这就是所有需要的。对于IIS,还需要告诉IIS如何处理这些URL(因为服务器首先分析了一个请求,以确定将其发送到哪里-包括是否将其发送到ASP.NET或其他扩展名)。

For your local dev webserver (embedded in Visual Studio) this is all that's needed. For IIS, you also need to tell IIS how to deal with these URLS (since the server first analyses a request to decide where to send it - including whether to send it to ASP.NET or some other extension).


  • 打开:IIS管理器->

  • 部分:网站->

  • 右键单击您的网站->

  • 选项:属性->

  • 标签:Home Directoy->

  • 按钮:[配置...]->

  • 选项卡:映射->

  • 部分:通配符应用程序映射(实现顺序): ->

  • 按钮:[插入...]->

  • 可执行文件: C:\WINDOWS\Microsoft.NET\ Framework\v2.0.50727\aspnet_isapi.dll(或您的处理程序使用的.NET运行时版本)->

  • 取消选中验证该文件是否存在->

  • 按钮:[确定]

  • Open: IIS Manager ->
  • Section: Websites ->
  • Right click on your website ->
  • Option: Properties ->
  • Tab: Home Directoy ->
  • Button: [Configuration...] ->
  • Tab: Mappings ->
  • Section: "Wildcard application maps (order of implementation):" ->
  • Button: [Insert...] ->
  • Executable: "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" (or whichever version of the .NET runtime your handler uses) ->
  • Uncheck "Verify that file exists" ->
  • Button: [OK]

现在IIS和ASP.NET都知道如何处理您的URL 。

Now both IIS and ASP.NET know how to deal with your URLS.

上述方法意味着,当请求静态文件时,ASP.NET实际上是在提供文件,而不是IIS,这会导致一些缺点(讨论此处)。您可以通过将目录切换到应用程序(在IIS管理器中),删除通配符映射语句(在上面添加)并从应用程序切回来覆盖此行为(从静态目录禁用通配符映射)。 Voilà-静态文件由IIS处理,而不会影响您的ASP.NET。

The above approach means when requesting static files, ASP.NET is actually serving the files, not IIS - which leads to a few disadvantages (discussed here). You can override this behaviour (disable wildcard mapping from the static directory) by switching the directory to an application (in IIS Manager), removing the wildcard mapping statement (added above) and switching it back from an application. Voilà - static files are handled by IIS without pestering your ASP.NET.