Configuration of ASP.NET AJAX

When you create the project of an AJAX-enabled ASP.NET Web site, everything looks like a classic ASP.NET application at first glance. After a second look, though, you can see that the configuration file contains some changes in the form of new sections and new runtime components. In particular, the runtime components-made-to-measure HTTP modules and HTTP handlers-play a key role in the implementation of ASP.NET AJAX.


The web.config File
In ASP.NET, the web.config file stores application settings that apply to the folder where it is located and to child subfolders. Each application can have a variety of web.config files to apply different settings at different folder levels.

The web.config file is a text file written in accordance with a well-known XML schema. The standard schema file features a built-in number of sections and elements, but new sections can be added to configure custom services and components. As mentioned, ASP.NET AJAX Extensions 1.0 is just an extension to ASP.NET, and it can be easily seen as a new service that requires its own set of extensions to the configuration syntax.


New Configuration Sections
The ASP.NET configuration file has a root element named <configuration>. A particular configuration file that contains information for a custom service can optionally define new sections. All nonstandard sections used in a configuration file must be declared in the initial <configSections> section. The following code snippet shows the new sections defined in the root web.config file of an ASP.NET AJAX application:

<configuration>
<configSections>
<sectionGroup name="system.web.extensions" ...>
<sectionGroup name="scripting" ... />
<section name="scriptResourceHandler" ... />
<sectionGroup name="webServices" ... >
<section name="jsonSerialization" ... />
<section name="profileService" ... />
<section name="authenticationService" ... />
</sectionGroup>
</sectionGroup>
</sectionGroup>
</configSections>
...
</configuration>

As you can see, everything goes under the <scripting> section which, in turn, is a child of <system.web.extensions>. Under the <scripting> section, you find another child section named <webServices>. Let’s dig out the <scripting> section first.


The <scripting> Section
The <scripting> section might contain a child section named <scriptResourceHandler>. The child section determines how ASP.NET AJAX system scripts should be served to the client and handled. The <scriptResourceHandler> section features two attributes: enableCaching and enableCompression. Both are Boolean values and are set to true by default:

<scripting>
<scriptResourceHandler enableCaching="true" enableCompression="true" />
</scripting>

The enableCaching attribute indicates whether the system script files (as well as any other linked scripts and resources) are to be cached locally or downloaded for each request. System script files are a key element of AJAX frameworks, and ASP.NET AJAX Extensions is no exception. The more the framework is rich with features, the more the size of such script files becomes large.
To keep the overall size of system script files below the threshold of a few KBs, compression is required. You can enable or disable compression through the enableCompression attribute. When the attribute is turned on, script files that are embedded as resources in an assembly and string resources are compressed.


The <webServices> Section
Under the <webServices> section, you find a number of child sections, as detailed in

• authenticationService - Configures the ASP.NET AJAX authentication service. With this service active, you can authenticate the user credentials without being redirected to a login page. Authentication can optionally be configured to occur only on secure channels.

• jsonSerialization - Defines the list of custom JSON serializers registered to work in the application. Serializers are grouped under the child <converters> section. The maxJsonLength attribute indicates maximum length in bytes for a JSON string. The default value is 500.

• profileService - Configures the ASP.NET AJAX profile service. With this service active, you can manipulate the properties of the ASP.NET user profile from JavaScript. To allow profile properties to be retrieved and modified from within ASP.NET AJAX applications, you add each property name to the readAccessProperties and writeAccessProperties attributes of the section.

The <webServices> section is not necessary if you don’t plan to use Web services from within ASP.NET AJAX pages.


The Runtime Engine
As an extension to ASP.NET, ASP.NET AJAX Extensions can’t help but rely on the extensibility model of ASP.NET. Some of its key functionalities are implemented using additional runtime components such as HTTP modules and handlers. When these components are required, you register them in the web.config file, as shown in the following sections.


The ScriptResource HTTP Handler
ASP.NET AJAX needs to register an HTTP handler to effectively manage any system and user-defined script that is being added to each page. The ASP.NET AJAX script manager injects the Microsoft AJAX library and other scripts via a made-to-measure handler named ScriptResource.axd. It is defined as follows:

<httpHandlers>
<add verb="GET,HEAD" path="ScriptResource.axd" validate="false"
type="System.Web.Handlers.ScriptResourceHandler,
System.Web.Extensions, ..." />
</httpHandlers>

If the validate attribute is false, ASP.NET does not attempt to load the specified handler class until an actual request comes for ScriptResource.axd. This behavior potentially delays any runtime errors, but it improves the startup time. Basically, the script resource handler works on JavaScript files and Web resources (images, strings) embedded into assemblies and optimizes the way in which those are injected into your client pages. The handler takes care of serving scripts efficiently for you. For example, it ensures that scripts are cached by the browser and compressed using GZip. Furthermore, it helps you with localization by reading the right version of the resource based on the current culture.
In addition, ASP.NET AJAX might require a second HTTP handler. The configuration code is shown here:

<httpHandlers>
<remove verb="*" path="*.asmx" />
<add verb="*" path="*.asmx" validate="false"
type="System.Web.Script.Services.ScriptHandlerFactory,
System.Web.Extensions, ..."/>
</httpHandlers>

As you can see, the default handler for ASP.NET Web services is removed (*.asmx files) and replaced with an AJAX-specific handler that falls back to the standard one in some cases. In particular, the new handler for *.asmx resources examines the ongoing HTTP request and lets it go as usual if it can’t find anything in the request that qualifies it as an ASP.NET AJAX request. You don’t need this handler if you’re not going to use ASP.NET Web services from within ASP.NET AJAX client pages.


The ScriptModule HTTP Module
The HTTP module named ScriptModule serves the purpose of executing remote page method calls in JavaScript from within client pages:

<httpModules>
<add name="ScriptModule"
type="System.Web.Handlers.ScriptModule, System.Web.Extensions, ..." />
</httpModules>

Without this module, you can’t invoke a properly decorated method on the ASP.NET codebehind class. Of course, if you don’t plan to do so, you don’t need register the ScriptModule HTTP module. This speeds up request processing slightly since the request isn’t routed through this module.

Source of Information : Microsoft Press Introducing Microsoft ASP NET AJAX

0 comments


Subscribe to Developer Techno ?
Enter your email address:

Delivered by FeedBurner