Tuesday, October 23, 2007

Updating ASP.NET projects to AJAX

OK, you have an ASP.NET project and decided to add the AJAX funcionality, using the ASP.NET AJAX 1.0 toolkit.

You downloaded the toolkit, installed, added the System.Web.Extensions reference, and, after you added the ScriptManager in your page, you got a script error telling that Sys was not an object.

Well, grasshopper, there is a little bit more than simply add the reference.

To really convert an ASP.NET application to use AJAX, you need not only to add the reference, but make the following changes on the Web.config file:
  • In the <configSections> add:
    <sectionGroup 
    name="system.web.extensions"
    type="System.Web.Configuration.SystemWebExtensionsSectionGroup,
    System.Web.Extensions,
    Version=1.0.61025.0,
    Culture=neutral,
    PublicKeyToken=31bf3856ad364e35">
    <sectionGroup
    name="scripting"
    type="System.Web.Configuration.ScriptingSectionGroup,
    System.Web.Extensions,
    Version=1.0.61025.0,
    Culture=neutral,
    PublicKeyToken=31bf3856ad364e35">
    <section
    name="scriptResourceHandler"
    type="System.Web.Configuration.ScriptingScriptResourceHandlerSection,
    System.Web.Extensions,
    Version=1.0.61025.0,
    Culture=neutral,
    PublicKeyToken=31bf3856ad364e35"
    requirePermission="false"
    allowDefinition="MachineToApplication"/>
    <sectionGroup
    name="webServices"
    type="System.Web.Configuration.ScriptingWebServicesSectionGroup,
    System.Web.Extensions,
    Version=1.0.61025.0,
    Culture=neutral,
    PublicKeyToken=31bf3856ad364e35">
    <section
    name="jsonSerialization"
    type="System.Web.Configuration.ScriptingJsonSerializationSection,
    System.Web.Extensions,
    Version=1.0.61025.0,
    Culture=neutral,
    PublicKeyToken=31bf3856ad364e35"
    requirePermission="false"
    allowDefinition="Everywhere" />
    <section
    name="profileService"
    type="System.Web.Configuration.ScriptingProfileServiceSection,
    System.Web.Extensions,
    Version=1.0.61025.0,
    Culture=neutral,
    PublicKeyToken=31bf3856ad364e35"
    requirePermission="false"
    allowDefinition="MachineToApplication" />
    <section
    name="authenticationService"
    type="System.Web.Configuration.ScriptingAuthenticationServiceSection,
    System.Web.Extensions,
    Version=1.0.61025.0,
    Culture=neutral,
    PublicKeyToken=31bf3856ad364e35"
    requirePermission="false"
    allowDefinition="MachineToApplication" />
    </sectionGroup>
    </sectionGroup>
    </sectionGroup>
  • Find or create the <httpHandlers> section and then add:
    <remove 
    verb="*"
    path="*.asmx"/>
    <add
    verb="*"
    path="*.asmx"
    validate="false"
    type="System.Web.Script.Services.ScriptHandlerFactory,
    System.Web.Extensions,
    Version=1.0.61025.0,
    Culture=neutral,
    PublicKeyToken=31bf3856ad364e35"/>
    <add
    verb="*"
    path="*_AppService.axd"
    validate="false"
    type="System.Web.Script.Services.ScriptHandlerFactory,
    System.Web.Extensions,
    Version=1.0.61025.0,
    Culture=neutral,
    PublicKeyToken=31bf3856ad364e35"/>
    <add
    verb="GET,HEAD"
    path="ScriptResource.axd"
    type="System.Web.Handlers.ScriptResourceHandler,
    System.Web.Extensions,
    Version=1.0.61025.0,
    Culture=neutral,
    PublicKeyToken=31bf3856ad364e35"
    validate="false"/>
  • Find or create the <httpModules> section and then add:
    <add 
    name="ScriptModule"
    type="System.Web.Handlers.ScriptModule,
    System.Web.Extensions,
    Version=1.0.61025.0,
    Culture=neutral,
    PublicKeyToken=31bf3856ad364e35"/>
  • Find or create the <system.webServer> section and then add:
    <validation validateIntegratedModeConfiguration="false"/>
    <modules>
    <add
    name="ScriptModule"
    preCondition="integratedMode"
    type="System.Web.Handlers.ScriptModule,
    System.Web.Extensions,
    Version=1.0.61025.0,
    Culture=neutral,
    PublicKeyToken=31bf3856ad364e35"/>
    </modules>
    <handlers>
    <remove
    name="WebServiceHandlerFactory-Integrated" />
    <add
    name="ScriptHandlerFactory"
    verb="*"
    path="*.asmx"
    preCondition="integratedMode"
    type="System.Web.Script.Services.ScriptHandlerFactory,
    System.Web.Extensions,
    Version=1.0.61025.0,
    Culture=neutral,
    PublicKeyToken=31bf3856ad364e35"/>
    <add
    name="ScriptHandlerFactoryAppServices"
    verb="*"
    path="*_AppService.axd"
    preCondition="integratedMode"
    type="System.Web.Script.Services.ScriptHandlerFactory,
    System.Web.Extensions,
    Version=1.0.61025.0,
    Culture=neutral,
    PublicKeyToken=31bf3856ad364e35"/>
    <add
    name="ScriptResource"
    preCondition="integratedMode"
    verb="GET,HEAD"
    path="ScriptResource.axd"
    type="System.Web.Handlers.ScriptResourceHandler,
    System.Web.Extensions,
    Version=1.0.61025.0,
    Culture=neutral,
    PublicKeyToken=31bf3856ad364e35" />
    </handlers>
And then, grasshopper, your ASP.NET application will be trully AJAX enabled.

0 Comments:

Post a Comment