ASP.NET 4.0 – New Features

By Joydip Kanjilal and Bhupali Khule ASP.NET 4.0 is neither a revolutionary change nor a refactoring of the existing ASP.NET. Instead, it consists of a number of small-scale changes that allow developers to have a strong control of certain frequently used events. What makes ASP.NET 4.0 more effective than the already existing versions of ASP.NET are its rich features and powerful enhancements.

ViewState Control Enhancements

One of the major drawbacks of ASP.NET Webforms is ViewState, which can add significantly to the page size and slow performance. While initially you could set the EnableViewState property to true or false, all the controls, by default inherit and so if you set it at the control level, the behavior was inconsistent. However, with ASP.NET 4.0, the new ViewStateMode property helps to determines every control whether the ViewState should be enabled, disabled, or inherited. Data Control Enhancements Another interesting new enhancement in ASP.NET 4.0 is that the LayoutTemplate of the ListView data control is now optional – so you can just use the ItemTemplate of the control to display data. Here’s an example:

<asp:ListView ID=”ListView1″ runat=”server”DataSourceID=”SqlDataSource1″

ClientIDMode=”Predictable”>

<ItemTemplate>

Student Code:

<asp:LabelID=”lblStudentCode” runat=”server” Text='<%# Eval(“StudentCode”)%>’ />

<br />

Student Name:

<asp:LabelID=”lblStudentName” runat=”server” Text='<%# Eval(“StudentName”)%>’ />

<br />

</ItemTemplate>

</asp:ListView>

In ASP.NET 3.5, you can achieve the same but you need to specify the layout template using the <layouttemplate> tag as shown below: <asp:ListView ID=”ListView1″ runat=”server” DataSourceID=”SqlDataSource1″ DataKeyNames=”StudentCode” ItemContainerID=”SqlDataSource1″> <layouttemplate> <table id=”studentTable” runat=”server” border=”1″> <tr> <th>StudentCode</th> <th>StudentName</th> </tr> </table> </layouttemplate> <ItemTemplate> <tr> <td> <asp:Label ID=”lblStudentCode” runat=”server” Text='<%# Eval(“StudentCode”) %>’/> </td> <td> <asp:Label ID=”lblStudentName” runat=”server” Text='<%# Eval(“StudentName”) %>’/>

</td> <td> </tr> </ItemTemplate> </asp:ListView>

Also, the RadioButtonList and the CheckBoxList controls now include a new property called RepeatLayout.

Webform Routing

Routing is a feature in ASP.NET 4.0 that enables you to use URLs to map specific resources. These URLs can then become more descriptive and user friendly. Here is an example of an URL that is descriptive: http://server/mywebapplication/Students/View/NewStudents Reading the routing information in an ASP.NET page is as simple as the following code: String data = Page.RouteData.Values[“Item”] as string; ASP.NET Routing was originally developed as part of ASP.NET MVC but factored out into its own assembly (System.Web.Routing) as it is useful beyond the bounds of MVC. It is used in ASP.NET Dynamic Data for example and it’s even possible to use it in webforms with a bit of work. In ASP.NET 4.0 all the necessary components to use ASP.NET Routing are inbuilt. These include the IRouteHandler implementation (PageRouteHandler class) that serves up the right IHttpHandler to service the request and a couple of expression builders to help capture parameters from routed requests and also generate route URLs. You simply need to set up your route patterns in Application_Start() as you do for ASP.NET MVC.

SEO Enhancements

In addition to Webforms Routing, there are a couple of minor additions to ASP.NET 4.0 to make it easier to set the Keywords and Description meta tags on a page. The page class in ASP.NET 4.0 now has two new properties, namely, Keywords and Description. These can be set either in markup code or from the code behind so that you can generate the meta tags dynamically. Here’s an example that illustrates how you can use these properties using code:

public partial class WebForm1 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

Page.Keywords = “ASP.NET 4.0”;

Page.Description = “Authors: Joydip Kanjilal and Bhupali Khule.”;

}

}

And here’s how you can do the same using markup code:

<html xmlns=”http://www.w3.org/1999/xhtml”>

<head>

<title>Programming ASP.NET 4.0</title>

<meta name=”description” content=”Authors: Joydip Kanjilal and Bhupali Khule.” />

<meta name=”keywords” content=”ASP.NET 4.0″ />

</head>

Simplified Web.Config File One of the most striking features in ASP.NET 4.0 is web.config minification – a feature that allows you to have a web.config file that is much smaller in size with fewer configuration elements compared to what we used to have in the earlier versions of ASP.NET. Most of the configuration elements now have been moved to the machine.config file resulting in a minified web.config file for your applications. Your applications can easily inherit the settings specified in the machine.config file. So, you can have a web.config file that is either empty or, much smaller in size. Here is how you can specify the target framework for your application’s web.config file in ASP.NET 4.0:– <?xml version=”1.0″?> <configuration> <system.web> <compilation targetFramework=”4.0″ /> </system.web> </configuration> Caching Enhancements Until now, any page output is stored in memory  in a private area of the ASP.NET cache. However, in the long run, the output cache puts additional pressure on the web server by consuming memory and generating frequent updates to the cache object. In ASP.NET 4.0, the output caching subsystem gives an opportunity to the developers to store page responses outside the ASP.NET worker process, by fully supporting the provider model. In ASP.NET 4.0, the Cache API enables you to use any of the following cache storages:

· Disk-based output caches – store cache data in disk

· Custom object caches – store cache data using custom cache providers

· Distributed object caches – store cache data on a separate server

· Cloud-based object caches – store cache data on a cloud database

To configure your custom output cache provider; you can specify the following in your application’s web.config file:

<caching>

<outputCachedefaultProvider=”TestProvider”>

<providers>

<add name=”DiskCache”

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

</providers>

</outputCache>

</caching>

You can also use a new feature called Output Cache Substitution to cache the output of web pages that contain dynamic data. For example: <asp:Substitution ID= “cacheSubstitution” runat=”server”MethodName=”Test” /> Session State Improvements In ASP.NET 4.0 you can use compression for out of process session state providers. Here is what you need to use this feature: <sessionState mode=”SqlServer” sqlConnectionString=”data source=joydip; Initial Catalog=aspnetstate” allowCustomSqlDatabase=”true” compressionEnabled=”true”/> Better Control over ClientID’s of Controls ASP.NET 4.0 provides support for customizing the Client ID that is generated by the ASP.NET engine for the controls in the web pages of your application. The new ClientIDMode property is now available for setting the client ID at either the Page, Application or Control levels. Note that you can set the ClientIDMode for any controls on your web page. You can even set the ClientIDMode for your web page. To set the ClientIDMode property at the page level, you can use the following code: <%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” ClientIDMode=”Predictable” %> The ClientIDMode property can hold one of the following possible values:

· AutoID – This is the legacy property, i.e., this will cause a client ID to be generated the same way as earlier versions of ASP.NET.

· Inherit – Inherit the clientID for a control from its parent. This is the default behavior for all controls.

· Static – This is used to specify the clientID of a control statically. So, whatever clientID you specify for the control will remain the same and the parent control’s ID and not be concatenated while generating the clientID for a control.

· Predictable – This option is used for specifying the clientID for controls that use repeating templates. In essence, this option is used to specify the clientID for controls to make the generated clientIDs unique and also predictable. You can use this option for specifying the clientID for data bound controls such as Repeater, GridView, etc.

To set the ClientIDMode at the application level, you can specify the following in your application’s web.config file:

<system.web>

<pages clientIDMode=”Predictable”></pages>

</system.web>

The following code snippet illustrates how you can set the ClientIDMode property at the control level: <asp:GridView runat=”server” ID=”GridView1″        AutoGenerateColumns=”False” ClientIDMode=”AutoID”> Support for Performance Monitoring Performance Monitoring is an area where ASP.NET 4.0 is way ahead from its earlier counterparts. Previously there was no direct way to analyze or monitor application performance. You can now use ASP.NET 4.0 to monitor resource functionality . ASP.NET 4.0 provides support for monitoring the performance of individual applications inside a single worker process. This feature provides a more granular view of the resources being consumed by your application. You can do this simply by using the following configuration in the aspnet.config file:

<configuration>

<runtime>

<appDomainResourceMonitoring enabled=”true”/>

</runtime>

</configuration>

Note that when you enable appDomainResourceMonitoring feature in your application, you can see two new performance counters in the “ASP.NET Applications Performance” category. These are: · % Managed Processor Time · Managed Memory Used References http://www.asp.net/LEARN/whitepapers/aspnet4/default.aspx http://blogs.msdn.com/mikeormond/archive/2009/05/06/asp-net-4-0-webforms-enhancements.aspx http://blogs.msdn.com/mikeormond/archive/2009/04/16/visual-studio-2010-enhancements-for-asp-net.aspx http://channel9.msdn.com/shows/The+Knowledge+Chamber/Stephen-Walther-New-Features-of-ASPNET-40/]]>

Leave a comment

Your email address will not be published.