Thursday, April 28, 2011

What's New in ASP.NET 4.0

1. In .NET Framework 4.0, the major configuration elements are moved to machine.config and applications now inherits these settings.
Sample of ASP.NET 4 Web.Config file









2. Output caching in ASP.NET 4 enables you to configure one or more custom output- cache providers. Output-cache providers can use any storage mechanism i.e. local or remote disks, cloud, distributed cache engine etc. to persist HTML content.
Example of specifying a different output-cahce provider for web page or web user control.


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


3. Some ASP.NET applications need to load large amount of data during initilization of the application before serving the first request. Prior to Asp.NET 4.0, we achieve the compilation of the application before the first request using the command Aspnet_compiler -v /MyWebSite. In Asp.NET 4.0, auto-start feature is available when ASP.NET 4 runs on IIS 7.5 on Windows Server 2008 R2.

You need to set following configuration in the ApplicationHost.config file.







If your application pool containing multiple applications, you can auto-start individual applications by setting the following configuration in applicationHost.config file.






The auto-start feature enables you to

  1. Start up the Application pool.
  2. Initialization of the Application
  3. It temporarily stops processing any http request till the initilization process is over.


4. The ASP.NET Redirect method results an extra round trip while doing a temporary redirect to an URL. ASP.NET 4.0 adds a new Helper method RedirectPermanent() that saves an unnecessary round trip.
While the Redirect method returns HTTP 302(Found) response, the RedirectPermanent() method issues HTTP 301(Permanent) response.

5. In Asp.net, The out of process Session State involves serialized session date sent to remote storage. It becomes a performance bottleneck if the data involved is large. The ASP.NET 4.0 Session State Compression feature helps to reduce the size of the data by compressing the Session State using GZipStream class. The Applications that have spare CPU cycles on the Web Server can achieve substantial performance improvement by using this feature.

You can set this option using the new compressionEnabled attribute of the sessionState element in the configuration file.

6. ASP.NET 4.0 allows you to configure the application URL path, query string length and invalid characters that are not allowed in the application url. You can configure them as follows.





7. The ASP.NET 4.0 System.Runtime.Caching API provides an uniform caching technique for all .NET applications (Web, Windows Form). It provides abstract types for custom cache implementation and a concrete in-memory object cache class called MemoryCache.

8. In ASP.NET 4, you can create custom encoding routines for the following common text-encoding tasks:

  • HTML encoding.
  • URL encoding.
  • HTML attribute encoding.
  • Encoding outbound HTTP headers.


Configure the custom encoding routines as follows:




9. CDN base Ajax and Jquery loading to ASP.NET 4.0 Applications.






[assembly: WebResource("Example.js", "application/x-javascript", CdnPath ="http://contoso.com/app/site/Example.js")]



10. 2 new properties are added to the Page class i.e. MetaKeywords, MetaDescription that helps set meta tags for search engine submission. The html rendered on the browser by these 2 properties is as follows:


<html>
<head>
<meta name="keywords" content="keyword1, keyword2"/>
<meta name="description" content="description"/>
</head>
<html>


11. A New property ViewStateMode is added to Control class. Setting ViewStateMode=false of the Control class disables viewstate for all the controls on the page. You can use ViewStateMode property of indiviual control to enable view state explicitly.

12. ASP.NET MVC way of routing in ASP.NET 4 Web Forms.

13. We use a Control's Client ID property to access that control in client script. Prior to ASP.NET 4.0, the Client ID is generated automatically by ASP.NET engine and sometimes it is difficult to predict the Client ID of a control to use in client script. In ASP.NET 4.0, you can specify how it can be generated by using ClientIDMode property at Page level or control level. It has following values:

Auto - It provides the same behaviour ASP.NET was providing prior to ASP.NET 4.0

Static - Instead of automatically generating Client ID, it sets the ID of the control as Client ID.

Predictable - For a Data Bound control, You can control how the ID of each instance of the control would be created by using ClientIDRowSuffix property. The value of this property will be appended to the Client ID property of each instance of the control.

Inherit - Inherit the ClientIDMode property of the container control to generate the Client ID of the control. If the control doesn't have a container control then it uses the ClientIDMode property of the Page.

The default mode for a site or page is - Predictable
The default mode for a control is - inherits

The following example shows how to set the default ClientIDMode for a site in the Web.config file.





The following example shows how to set the default ClientIDMode for a page in the @ Page directive.


<%@ Page Language="C#" CodeFile="Default.aspx.cs"

Inherits="_Default" ClientIDMode="Static" %>


14. Persisting row selection in Data Controls - Rows selected in Pages will be persisted across pages. When you select a row 3 on Page1 and move to Page8. If you go back to Page1, you will see row3 as selected. To achieve this, use PersistedSelection property of the Data Control.






15. Filtering date with the QueryExtender control. The Query Extender control can be added to EntityDataSource or LinqDataSource to filter the data. The QueryExtender has following filter options-


ContextTypeName="AdventureWorksDataContext" EntityTypeName=""
TableName="Products" >



SearchExpression -
RangeExpression-
PropertyExpression-
OrberByExpression-
CustomExpression-


<html>
<head runat="server">
<title>Filter Demo</title>
</head>
<body>
<form id="form1" runat="server">
Search:<asp:TextBox ID="SearchTextBox" runat="server" />
<p>

Make More:<asp:CheckBox ID="MakeCheckBox" runat="server" />
<p>
From:<asp:TextBox ID="FromTextBox" runat="server" ></asp:TextBox>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
To:<asp:TextBox ID="ToTextBox" runat="server" ></asp:TextBox>
</p>
<p>
<asp:Button ID="Button1" runat="server" Text="Search" />
</p>
<asp:LinqDataSource ID="LinqDataSource1"
ContextTypeName="FilterDemo.AdventureWorksDataContext"
TableName="Products" runat="server">
</asp:LinqDataSource>
<asp:QueryExtender runat="server" TargetControlID="LinqDataSource1">

<asp:SearchExpression SearchType="StartsWith" DataFields="Name" >
<asp:ControlParameter ControlID="SearchTextBox" />
</asp:SearchExpression>

<asp:RangeExpression DataField="ReorderPoint" MinType="Inclusive" MaxType="Inclusive">
<asp:ControlParameter ControlID="FromTextBox" />
<asp:ControlParameter ControlID="ToTextBox" />
</asp:RangeExpression>

<asp:PropertyExpression>
<asp:ControlParameter ControlID="MakeCheckBox" Name="MakeFlag" />
</asp:PropertyExpression>
</asp:QueryExtender>

<asp:GridView ID="GridView1" runat="server"
DataSourceID="LinqDataSource1" AllowPaging="True"
DataKeyNames="ProductID>
</asp:GridView>
</form>

</body>
</head>
</html>

No comments:

Post a Comment