Tuesday, 21 July 2009

Ajax Script Handlers in IIS 7 (jQuery, Ajax, ASMX)

There’s a fair bit of information about how to get jQuery to work with ajax calls to ASMX files using JSON.  There are lots of ways to do it, and most of it is specifically focussed on IIS 6.  Well I came across a couple of problems in IIS 7 when trying to deploy an ajax script that could consume an ASMX web service.

One thing that you will need to do is enable the HTTP handler for ajax and ASMX.  In IIS 6 this is different from IIS 7, but it can be really hard to find the correct resource on how to do it correctly with IIS 7.

In IIS 6 you do something along the lines of:

   1: <httpHandlers>
   3: <remove verb="*" path="*.asmx"/>
   4: <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
   6: </httpHandlers>
However IIS 7 has changed a little and you’ll need to define your script handler more like this:

   1: <system.webServer>
   3:     <handlers>
   5:       <remove name="ScriptHandlerFactory"/>
   6:       <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
   8:     </handlers>
  10: </system.webServer>

I’m sure most of you know this already, but IIS 7 is relatively new to me compared to IIS 6 so this took me a while to find.

No comments: