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″
<asp:LabelID=”lblStudentCode” runat=”server” Text=’<%# Eval(“StudentCode”)%>’ />
<asp:LabelID=”lblStudentName” runat=”server” Text=’<%# Eval(“StudentName”)%>’ />
<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 runat=”server” DataSourceID=”SqlDataSource1″ DataKeyNames=”StudentCode” ItemContainerID=”SqlDataSource1″>
<table runat=”server” border=”1″>
Text=’<%# Eval(“StudentCode”) %>’/>
Text=’<%# Eval(“StudentName”) %>’/>
Also, the RadioButtonList and the CheckBoxList controls now include a new property called RepeatLayout.
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 theIRouteHandler 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.
For a full details see ASP.NET 4.0 Routing .
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:
<title>Programming ASP.NET 4.0</title>
<meta content=”Authors: Joydip Kanjilal and Bhupali Khule.” />
<meta content=”ASP.NET 4.0″ />
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:–
<compilation targetFramework=”4.0″ />
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’sweb.config file:
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 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