Wednesday, July 30, 2008

Dreaded 'Sys' is undefined error when using ASP.Net AJAX

If you are using ASP.Net AJAX for the first time, no doubt you will come across the 'Sys' is undefined error while working with an AJAX control.

After a lot of scratching my head I figured out what caused it and how to fix it.

Typically you are going to be hit with this error when you are working with a plain ASP.Net website. (i.e., the project was created by selecting Asp.Net website in the New Project dialog).

image

Instead if you created the project by selecting ASP.Net AJAX-Enabled web site, I dont think you will see this error - as VS will setup your project correctly so that the error will not occur.

Ok - now what do you do if you have an existing project that was created by selecting just the "ASP.Net Web Site" option.

Turns out that all the errors arise from the Web.Config file not having all the correct entries required for you to "AJAXify" the web site.

Here is what you do:

(I am assuming that your project already contains a web.config file)

If you look in the ASP.Net AJAX install folder you will find a sample web.config that shows all the configuration items that you need to setup. (This is typically the folder - C:\Program Files\Microsoft ASP.NET\ASP.NET 2.0 AJAX Extensions\currentVersion)

Under <configuration><configSections> add the following text.

<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>

Next under <system.web> add the following text

<pages>
      <controls>
        <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      </controls>
</pages>

Also under <system.web> make sure that you have the following entry (which goes under <system.web><compilation><assemblies>:

<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

Also make sure that <system.web> has the following entries for httpHandlers and httpModules

<httpHandlers>
      <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"/>
    </httpHandlers>

    <httpModules>
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    </httpModules>

And that should be it. You should be in business and your web site should be AJAXed.

Final word, after googling this error - I found that there might be many different reasons for this error. This one is the most rudimentary and worth testing out first, before going down a more in-depth path in trying to determine the source of this issue.

Here is an excellent blog post that goes into the depths about this error - http://weblogs.asp.net/chrisri/archive/2007/02/02/demystifying-sys-is-undefined.aspx

1 comment:

Remember, if you want me to respond to your comment, then you need to use a Google/OpenID account to leave the comment.