代码改变世界

Working with HttpHandler in IIS 7

2012-01-13 13:45  ~波  阅读(301)  评论(0编辑  收藏  举报

link:http://www.dotnetfunda.com/articles/article112.aspx

In order to use custom HttpHandler in IIS 6 we simply used to write <httpHandlers> tag inside System.web tag in web.config file but in IIS 7 it is little tricky. In this article, I am going to show how to configure custom HttpHanlder in IIS 7.

Introduction

If you had used Custom HttpHandler in your application that was hosted on IIS 6 or on Windows XP system and have shifted to IIS 7 or Vista now, your HttpHanlder will not work. You will have to do a little trick in your web.config file either manually or through IIS 7. (To know more about how to write Custom HttpHanlder to form SEO friendly url, visithttp://www.dotnetfunda.com/articles/article75.aspx )

Configuring Custom HttpHanlder in IIS 7

There are two ways you can configure your Custom HttpHandler in IIS 7.

First way

The first way is by going through Wizard in IIS. Lets see how to do that.

  1. First open your IIS 7.
  2. Got to your website
  3. Open Handler Mappings by double clicking it.
  4. Click Add Managed Handlers ... from Action pane (right side), you should see your screen something like Picture - 1 below.
  5. Write Request Path
  6. Write Type of the handler you want to use for the requested path
  7. Write Name of this handler for identification purpose.
  8. Click OK

Picture - 1

As soon as you will click OK, you can notice that your application web.config file has been changed. In my case my code inside web.config file looks like following

<system.webServer>

<handlers>

<add name="MyCustomHanlder" path="/mypage/mypage*.aspx" verb="*" type="MyHanlder.CustomHttpHanlder" resourceType="Unspecified" preCondition="integratedMode" />

</handlers>

<system.webServer>

Notice that earlier we used to write handlers inside <system.web> > <httpHandlers> but here it is written in <system.webServer> > <handlers>. Once your web.config file has been modified, you are done. Now simply test it. Try any url like http://localhost/mypage/mypageWHATEVER.aspx (in my case) and you will see that your hanlder is working now.

Second way

The second way is very easy, simply copy the code of the hanlder you just added and modify their corresponding attributes and place inside <handlers> and it will work. So there is no need to go to IIS every time to add new handlers.

Hope you liked this small but useful article.

Thanks and keep reading !!!