Microsoft.NET

……………………………………………….Expertise in .NET Technologies

ASP.NET 4.0 and Visual Studio 2010 Web Development – Part 1

Posted by Ravi Varma Thumati on May 27, 2010

Many exciting changes for ASP.NET are coming in the .NET Framework version 4.0. This document gives an overview of many of the new features that are included in the upcoming Beta 1 release of .NET 4.0 and in the Visual Studio 2010 release.

Contents

Core Services. 2

Extensible Output Caching. 2

Auto-Start Web Applications. 4

Permanently Redirecting a Page. 5

The Incredible Shrinking Session State. 6

AJAX Functionality in ASP.NET 4.0. 6

Client Template Rendering. 7

Instantiating Behaviors and Controls Declaratively. 9

Live Data Binding. 11

Using the Observer Pattern with JavaScript Objects and Arrays. 13

The DataView Control 14

The AdoNetServiceProxy Class. 16

The DataContext and AdoNetDataContext Classes. 16

Refactoring the Microsoft AJAX Framework Libraries. 17

Web Forms. 20

Setting Meta Tags with the Page.Keywords and Page.Description Properties. 20

Enabling View State for Individual Controls. 21

Changes to Browser Capabilities. 23

Routing in ASP.NET 4.0. 27

Setting Client IDs. 31

Persisting Row Selection in Data Controls. 34

FormView Control Enhancements. 35

ListView Control Enhancements. 36

Filtering Data with the QueryExtender Control 36

Dynamic Data. 38

Declarative DynamicDataManager Control Syntax. 39

Entity Templates. 39

New Field Templates for URLs and E-mail Addresses. 41

Creating Links with the DynamicHyperLink Control 41

Support for Inheritance in the Data Model 42

Support for Many-to-Many Relationships (Entity Framework Only). 42

New Attributes to Control Display and Support Enumerations. 42

Enhanced Support for Filters. 42

Visual Studio 2010 Web Designer Improvements. 43

Improved CSS Compatibility. 43

HTML and JScript Snippets. 43

JScript IntelliSense Enhancements. 43

Web Application Deployment with Visual Studio 2010. 43

Web Packaging. 44

Web.Config Transformation. 45

Database Deployment. 45

One-Click Publishing. 45

Resources. 45

Disclaimer. 47

Core Services

ASP.NET 4 introduces a number of features that improve core ASP.NET services such as output caching and session-state storage.

Extensible Output Caching

Since ASP.NET 1.0 was released, output caching has enabled developers to store the generated output of pages, controls, and HTTP responses in memory. On subsequent Web requests, ASP.NET can serve content more quickly by retrieving the generated output from memory instead of regenerating the output from scratch. However, this approach has a limitation — generated content always has to be stored in memory, and on heavily trafficked servers, the memory consumed by output caching can compete with memory demands from other portions of a Web application.

ASP.NET 4.0 adds an extensibility point to output caching that enables you to configure one or more custom output-cache providers. Output-cache providers can use any storage mechanism to persist HTML content. This makes it possible to create custom output-cache providers for diverse persistence mechanisms, which can include local or remote disks, cloud storage, and distributed cache engines.

You create a custom output-cache provider as a class that derives from the new System.Web.Caching.OutputCacheProvider type. You can then configure the provider in the Web.config file by using the new providers subsection of the outputCache element, as shown in the following example:

<caching>

<outputCache defaultProvider=”AspNetInternalProvider”>

<providers>

<add name=”DiskCache”

type=”Test.OutputCacheEx.DiskOutputCacheProvider, DiskCacheProvider”/>

</providers>

</outputCache>

</caching>

By default in ASP.NET 4.0, all HTTP responses, rendered pages, and controls use the in-memory output cache, as shown in the previous example, where the defaultProvider attribute is set to AspNetInternalProvider. You can change the default output-cache provider used for a Web application by specifying a different provider name for defaultProvider.

In addition, you can select different output-cache providers per control and per request. The easiest way to choose a different output-cache provider for different Web user controls is to do so declaratively by using the new providerName attribute in a page or control directive, as shown in the following example:

<%@ OutputCache Duration=”60″ VaryByParam=”None” providerName=”DiskCache” %>

Specifying a different output cache provider for an HTTP request requires a little more work. Instead of declaratively specifying the provider, you instead override the new GetOuputCacheProviderName method in the Global.asax file to programmatically specify which provider to use for a specific request. The following example shows how to do this.

public override string GetOutputCacheProviderName(HttpContext context)

{

if (context.Request.Path.EndsWith(“Advanced.aspx”))

return “DiskCache”;

else

return base.GetOutputCacheProviderName(context);

}

With the addition of output-cache provider extensibility to ASP.NET 4.0, you can now pursue more aggressive and more intelligent output-caching strategies for your Web sites. For example, it is now possible to cache the “Top 10” pages of a site in memory, while caching pages that get lower traffic on disk. Alternatively, you can cache every vary-by combination for a rendered page, but use a distributed cache so that the memory consumption is offloaded from front-end Web servers.

Auto-Start Web Applications

Some Web applications need to load large amounts of data or perform expensive initialization processing before serving the first request. In earlier versions of ASP.NET, for these situations you had to devise custom approaches to “wake up” an ASP.NET application and then run initialization code during the Application_Load method in the Global.asax file.

A new scalability feature named auto-start that directly addresses this scenario is available when ASP.NET 4.0 runs on IIS 7.5 on Windows Server 2008 R2. The auto-start feature provides a controlled approach for starting up an application pool, initializing an ASP.NET application, and then accepting HTTP requests.

To use the auto-start feature, an IIS administrator sets an application pool in IIS 7.5 to be automatically started by using the following configuration in the applicationHost.config file:

<applicationPools>

<add name=”MyApplicationPool” startMode=”AlwaysRunning” />

</applicationPools>

Because a single application pool can contain multiple applications, you specify individual applications to be automatically started by using the following configuration in the applicationHost.config file:

<sites>

<site name=”MySite” id=”1″>

<application path=”/”

preloadEnabled=“true”

preloadProvider=“PrewarmMyCache” >

<!– Additional content –>

</application>

</site>

</sites>

<!– Additional content –>

<preloadProviders>

<add name=“PrewarmMyCache”

type=“MyNamespace.CustomInitialization, MyLibrary” />

</preloadProviders>

When an IIS 7.5 server is cold-started or when an individual application pool is recycled, IIS 7.5 uses the information in the applicationHost.config file to determine which Web applications need to be automatically started. For each application that is marked for auto-start, IIS7.5 sends a request to ASP.NET 4.0 to start the application in a state during which the application temporarily does not accept HTTP requests. When it is in this state, ASP.NET instantiates the type defined by the preloadProvider attribute (as shown in the previous example) and calls into its public entry point.

You create a managed auto-start type with the necessary entry point by implementing the IProcessHostPreloadClient interface, as shown in the following example:

public class CustomInitialization : System.Web.Hosting.IProcessHostPreloadClient

{

public void Preload(string[] parameters)

{

// Perform initialization.

}

}

After your initialization code runs in the Preload method and the method returns, the ASP.NET application is ready to process requests.

With the addition of auto-start to IIS 7.5 and ASP.NET 4.0, you now have a well-defined approach for performing expensive application initialization prior to processing the first HTTP request. For example, you can use the new auto-start feature to initialize an application and then signal a load-balancer that the application was initialized and ready to accept HTTP traffic.

Permanently Redirecting a Page

It is common practice in Web applications to move pages and other content around over time, which can lead to an accumulation of stale links in search engines. In ASP.NET, developers have traditionally handled requests to old URLs by using by using the Response.Redirect method to forward a request to the new URL. However, the Redirect method issues an HTTP 302 Found (temporary redirect) response, which results in an extra HTTP round trip when users attempt to access the old URLs.

ASP.NET 4.0 adds a new RedirectPermanent helper method that makes it easy to issue HTTP 301 Moved Permanently responses, as in the following example:

RedirectPermanent(“/newpath/foroldcontent.aspx”);

Search engines and other user agents that recognize permanent redirects will store the new URL that is associated with the content, which eliminates the unnecessary round trip made by the browser for temporary redirects.

The Incredible Shrinking Session State

ASP.NET provides two default options for storing session state across a Web farm:  a session-state provider that invokes an out-of-process session-state server, and a session-state provider that stores data in a Microsoft SQL Server database. Because both options involve storing state information outside a Web application’s worker process, session state has to be serialized before it is sent to remote storage. Depending on how much information a developer saves in session state, the size of the serialized data can grow quite large.

ASP.NET 4.0 introduces a new compression option for both kinds of out-of-process session-state providers. When the compressionEnabled configuration option shown in the following example is set to true, ASP.NET will compress (and decompress) serialized session state by using the .NET Framework System.IO.Compression.GZipStream class.

<sessionState

mode=”SqlServer”

sqlConnectionString=”data source=dbserver;Initial Catalog=aspnetstate”

allowCustomSqlDatabase=”true”

compressionEnabled=“true”

/>

With the simple addition of the new attribute to the Web.config file, applications with spare CPU cycles on Web servers can realize substantial reductions in the size of serialized session-state data.

Advertisements

2 Responses to “ASP.NET 4.0 and Visual Studio 2010 Web Development – Part 1”

  1. mark coble said

    Just want to say what a great blog you got here!
    I’ve been around for quite a lot of time, but finally decided to show my appreciation of your work!

    Thumbs up, and keep it going!

  2. mark coble said

    Hey I like the blog. reads great. Do you use Twitter as well?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: