Introduction
In this article, I want to give an overview of the new features of ASP.NET that are included in the .NET framework 4 and Visual Studio 2010. I will explain these new features from the problem/solution strategy.
New features
1. Extensible Output Caching
- Context: What is cache? A cache is a memory area where the output of operations is stored in order to improve the performance. When we need this output again, we look for in the cache unlike to execute the operations again.
- Problem: This approach has a memory limitation. If your servers are experiencing heavy traffic, then the memory consumed by the output caching can compete with memory demands from other applications or components of your own application.
- Solution: The extensible output caching enables you to configure one or more custom output-cache providers for diverse persistence mechanisms such as hard drives (locally or remotely), cloud storage and distributed cache engines as Velocity.
Implementation strategy: You can create a custom output-cache provider as a class that derives from the new System.Web.Caching.OutputCacheProvider type. For example, let's create a provider using AppFabric (Velocity) as shown in the Listing 1.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;
using
Microsoft.ApplicationServer.Caching;
namespace
_01WebAppOutputCacheDemo.OutputCacheProviders
{
public class VelocityCacheProvider
: OutputCacheProvider, IDisposable
{
private
DataCache dataCache;
const String OutputCacheName = "OutputCache";
public
VelocityCacheProvider()
{
DataCacheFactory
factory = new DataCacheFactory();
this.dataCache
= factory.GetCache(OutputCacheName);
}
public override object Add(string key, object
entry, DateTime utcExpiry)
{
this.dataCache.Add(key,
entry, utcExpiry - DateTime.UtcNow);
return
entry;
}
public override object Get(string key)
{
return
this.dataCache.Get(key);
}
public override void Remove(string key)
{
this.dataCache.Remove(key);
}
public override void Set(string key, object
entry, DateTime utcExpiry)
{
throw
new NotImplementedException();
}
public void Dispose()
{
this.dataCache = null;
}
}
}
Listing 1
You can then configure the provider in the Web.config file by using the new provider subsection of the outputCache element (see Listing 2).
<caching>
<outputCache defaultProvider="AspNetInternalProvider">
<providers>
<add name="VelocityCache" type="_01WebAppOutputCacheDemo.OutputCacheProviders.VelocityCacheProvider,
01WebAppOutputCacheDemo"/>
</providers>
</outputCache>
</caching>
Listing 2
Moreover, 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 control directive (see Listing 3).
<%@ OutputCache Duration="60" VaryByParam="None" providerName="VelocityCache" %>
Listing 3
Instead of declaratively specifying the provider, you override the new GetOuputCacheProviderName method in the Global.asax file to programmatically specify which provider to use for a specific request (see Listing 4).
public override string
GetOutputCacheProviderName(HttpContext
context)
{
if
(context.Request.Path.EndsWith("CacheWebForm.aspx"))
return "VelocityCache";
else
return base.GetOutputCacheProviderName(context);
}
Listing 4
2. Permanently Redirecting a Page
Context: 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.
Problem: Traditionally, this is done using the Response.Redirect method to forward a request to the new URL. However, this 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.
Solution: ASP.NET 4 adds a new RedirectPermanent method that makes it easy to issue HTTP 301 Moved Permanently responses.
Implementation strategy: See an example in the Listing 5.
Response.RedirectPermanent("newlocation/page.aspx");
Listing 5
3. Shrinking Session State
Context: It is common practice in Web applications to store objects in the session state.
Problem: Depending on how much information a developer saves in session state, the size of the serialized data can grow quite large.
Solution: ASP.NET 4 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.
Implementation strategy: See an example in the Listing 6.
<sessionState
mode="SQLServer" sqlConnectionString="data
source=dbserver;Initial Catalog=aspnetstate" allowCustomSqlDatabase="true" compressionEnabled="true"/>
Listing 6
4. Setting Meta Tags
Context: The Meta tags enable you including reference information about the Web page (metadata) such as author, keywords, content, etc.
Problem: We need to dynamically include metadata in our page, for example, from a relational data source for the search engine to give more relevance to our site. This is a Search Engine Optimization (SEO) technique.
Solution: ASP.NET 4 introduces the new properties MetaKeywords and MetaDescription to the Page class. The @Page directive contains the Keywords and Description attribute.
Implementation strategy: See an example in the Listing 7.
<%@ Page Title="Home Page"
Language="C#"
MasterPageFile="~/Site.master"
AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="_01WebAppOutputCacheDemo._Default" Keywords="This is the
default page" Description="This is the default page" %>
Listing 7
This configuration generates the following output as shown in the Listing 8.
<head id="Head1" runat="server">
<title>Untitled Page</title>
<meta name="keywords" content="These, are, my, keywords" />
<meta name="description" content="This is the description of my page" />
</head>
Listing 8
5. Better control of the ViewState
Problem: When we use the ViewState to store a great deal of data, this causes a signficant performance degradation.
Solution: ASP.NET 4 introduces the new property ViewStateMode in the Web controls that lets you disable view state by default and then enable it only for the controls that require it in the page. This property may have three values: Enable, Disable, and Inherit. You can also set the ViewStateMode at the page level.
Implementation strategy: See an example in the Listing 9.
<%@ Page Title="Home Page"
Language="C#"
MasterPageFile="~/Site.master"
AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="_01WebAppOutputCacheDemo._Default" ViewStateMode="Disabled"
%>
Listing 9
6. Routing in ASP.NET 4
Context: To increase the traffic to our Web site, one of the most used SEO (search engine opitimization) technique is the URL normalization in order to add semantic to the URL, for example, a Web page that displays a list of products by category is traditionally accessed by the URL http://website/products.aspx?categoryid=12, if we need the URL to be descriptive, we have to transform it as http://website/products/categoryid/12.
Problem: How can we configure our applications using Web Form pages to use URL not bind to physical files in the server?
Solution: Although routing features comes with ASP.NET 3.5 SP1, ASP.NET 4 comes with new additions to make it easier to use the routing mechanisms, including the following:
- The PageRouteHandler class, which is a simple HTTP handler that you use when you define routes. The class passes data to the page that the request is routed to.
- The new properties HttpRequest.RequestContext and Page.RouteData (which is a shortcut to HttpRequest.RequestContext.RouteData). These properties make it easier to access information that is passed from the route.
- The following new expression builders, which are defined in System.Web.Compilation.RouteUrlExpressionBuilder and System.Web.Compilation.RouteValueExpressionBuilder:
- RouteUrl, which provides a simple way to create a URL that corresponds to a route URL within an ASP.NET server control.
- RouteValue, which provides a simple way to extract information from the RouteContext object.
- The RouteParameter class, which makes it easier to pass data contained in a RouteContext object to a query for a data source control (similar to FormParameter).
Implementation strategy:
This is the main steps to add routing capabilities to your application.
- Open the Global.asax file and add the following code to bind the products/category/{id} url pattern with the physical page ProductsCategory.aspx (see Listing 10).
Using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Routing;
namespace _02WebAppRoutingDemo
{
public class Global : System.Web.HttpApplication
{
// New code added
void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("ProductsCategory", "products/category/{id}", "~/ProductsCategory.aspx");
}
void Application_Start(object sender, EventArgs e)
{
// New code added
RegisterRoutes(RouteTable.Routes);
}
}
}
Listing 10 - And finally, let's create the ProductsCategory.aspx file and add the code to get category value and display in the page (see Listing 11).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace _02WebAppRoutingDemo
{
public partial class ProductsCategory : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string productCategoryId = this.Page.RouteData.Values["id"]!=null?this.Page.RouteData.Values["id"].ToString():"No Product Category";
this.lblProductCategoryId.Text = productCategoryId;
}
}
}
Listing 11
7. Setting Client IDs
Context: Everyday, we use Ajax and JavaScript libraries to build Rich Internet Applications (RIA). The new ClientIDMode property addresses a long-standing issue in ASP.NET, namely how controls create the id attribute for elements that they render. Knowing the id attribute for rendered elements is important if your application includes client script that references these elements.
Problem: Until ASP.NET 4, the algorithm for generating the id attribute from the ClientID property has been to concatenate the naming container (if any) with the ID, and in the case of repeated controls (as in data controls), to add a prefix and a sequential number. While this has always guaranteed that the IDs of controls in the page are unique, the algorithm has resulted in control IDs that were not predictable, and were therefore difficult to reference in client script. This occurs specially when we're using master pages.
A trick to get a reference to the HTML elements is shown in the Listing 12:
var btn = document.getElementById("<% =Button1.ClientID %>");
Listing 12
Or, the more elegant solution in jQuery as shown in the Listing 13.
var btn = $('<% =Button1.ClientID %>');
Listing 13
Solution: ASP.NET 4 introduces new ClientIDMode property lets you specify more precisely how the client ID is generated for controls. You can set the ClientIDMode property for any control, including for the page. Possible settings are the following:
- AutoID. This is equivalent to the algorithm for generating ClientID property values that was used in earlier versions of ASP.NET.
- Static. This specifies that the ClientID value will be the same as the ID without concatenating the IDs of parent naming containers. This can be useful in Web user controls. Because a Web user control can be located on different pages and in different container controls, it can be difficult to write client script for controls that use the AutoID algorithm because you cannot predict what the ID values will be.
- Predictable. This option is primarily for use in data controls that use repeating templates. It concatenates the ID properties of the control's naming containers, but generated ClientID values do not contain strings like "ctlxxx". This setting works in conjunction with the ClientIDRowSuffix property of the control. You set the ClientIDRowSuffix property to the name of a data field, and the value of that field is used as the suffix for the generated ClientID value. Typically you would use the primary key of a data record as the ClientIDRowSuffix value.
- Inherit. This setting is the default behavior for controls; it specifies that a control's ID generation is the same as its parent.
Implementation strategy:
An AutoID example (see Listing 14).
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent"
runat="server">
<asp:Panel ID="PanelParent" runat="server">
<asp:Panel ID="PanelChild" runat="server">
<asp:TextBox ID="txtEcho" runat="server" ClientIDMode="AutoID" />
</asp:Panel>
</asp:Panel>
</asp:Content>
Listing 14
And the output is shown in the Listing 15.
<div id="MainContent_PanelParent">
<div id="MainContent_PanelChild">
<input name="ctl00$MainContent$txtEcho" type="text"
id="ctl00_MainContent_txtEcho"
/>
</div>
</div>
Listing 15
A Static example (see Listing 16).
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent"
runat="server">
<asp:Panel ID="PanelParent" runat="server">
<asp:Panel ID="PanelChild" runat="server">
<asp:TextBox ID="txtEcho" runat="server" ClientIDMode="Static" />
</asp:Panel>
</asp:Panel>
</asp:Content>
Listing 16
The output is shown in the Listing 17.
<div id="MainContent_PanelParent">
<div id="MainContent_PanelChild">
<input name="ctl00$MainContent$txtEcho"
type="text"
id="txtEcho"
/>
</div>
</div>
Listing 17
A Predictable example (see Listing 18).
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent"
runat="server">
<asp:Panel ID="PanelParent" runat="server">
<asp:Panel ID="PanelChild" runat="server">
<asp:TextBox ID="txtEcho" runat="server" ClientIDMode="Predictable" />
</asp:Panel>





Niranjan PoddarPosted Jul 8, 2017, 2:47 AM
Thanks for this article.........
Vithal WadjePosted Sep 25, 2014, 2:27 PM
super
Abhishek KumarPosted Sep 19, 2014, 7:28 AM
Great Article Charles...
Chandra Sekhar KPosted Dec 1, 2012, 10:14 AM
Thanks for This article
JoshiPosted Nov 5, 2012, 1:58 AM
this validation is server side, because of that every time hit the server and refresh page.is possible like validation control's ?
vipin kumarPosted Aug 26, 2012, 2:16 AM
Thanks for This article please send to me other All new changes in Visual studio, 2010 which is not available in VS 2008
Abdul SamiPosted May 9, 2012, 4:25 PM
There is one good article about new features of asp.net 4 on http://www.codeproject.com/Articles/49938/What-is-New-in-ASP-NET-4-0-Visual-Studio-2010-IDE
Tejas MehtaPosted Jul 20, 2010, 2:44 AM
Helpfull features...thanx nd keep updating us...
Mike GoldPosted Jul 6, 2010, 10:53 PM
Thanks Charles!