ASP.NET 4.0 Features - Visual Studio 2010 Imprimare
Scris de Administrator   
Miercuri, 12 Mai 2010
Vizualizări: 4063

by Ludmal De Silva
      
ASP.NET v4 is released with Visual studio 2010. Web developers are presented with a bewildering range of new features and so Ludmal De Silva has described what he considers to be the most important new features in ASP.NET V4
He focus of Microsoft’s latest ASP.NET 4 has mainly been on improving the performance and Search-engine Optimization (SEO). In this article, I'll be taking a look at what I think are the most important new features in ASP.NET 4.
  • Output cache extensibility
  • Session state compression
  • View state mode for individual control
  • Page.MetaKeyword and Page.MetaDescription properties
  • Response.RedirectPermanent method
  • Routing in ASP.NET
  • Increase the URL character length
  • New syntax for Html Encode
  • Predictable Client IDs
  • Web.config file refactoring
  • Auto-Start ASP.NET applications
  • Improvements on Microsoft Ajax Library

I’ll describe the details of each of these features in the following sections.

 

Output Cache extensibility

Output caching, or Page-Level Caching, caches the entire rendered markup of an ASP.NET web page for a specific time-period. This has always been one of the essential features for ASP.NET that is used extensively to increase application performance. However there have been some limitations on the feasible extent of caching, because cached content always had to be stored in-memory.

But with ASP.NET 4.0 developers can extend their caching by using Output-cache providers. Developers can now create ‘output-cache providers’ that store the cache contents to any persistence mechanism such as databases, disks, cloud storage and distributed cache engines.

 

To create a custom output-cache provider, a class which derived fromSystem.Web.Caching.OutputCacheProvider has to be created in ASP.NET 4.0. There are four public methods which you have to override in order to provide your own implementation for add, remove, retrieve and update functionality. Also, the output-cache provider has to be registered in the web.config file as shown in the following screen capture.

 

 

You can also set this custom output-cache provider as your default cache mechanism. So once you add the page cache directives all of your contents will be stored using the custom output-cache provider.

 

 

Moreover, developers can also dynamically configure which output-cache Provider is used. For example you might want to cache the frequently access pages in the memory for faster access and less frequent pages on disk. By overriding the GetOutputCacheProviderName() method you can configure which output cache provider to use for different requests. These additions to the output-cache can enable developers to write extensible and more efficient cache mechanisms to their web application and thereby improve its responsiveness.
Session State compression
The ASP.NET session state is a mechanism to maintain session-specific data through subsequent requests. In some instances, you may wish to store your session state data in a session-state server or in Microsoft SQL server. However, these two options require you to store data out of the web application’s worker process. To send across to the relevant sources, (State server or Microsoft SQL Server), session-state data has to be serialized. This can take a significant time if the size of the data to be serialized grows significantly. This will increase the latency of the application.

 

This latency can be reduced if the size of the data is lessened by compression. ASP.NET 4.0 introduces a new mechanism to compress your session state data for both Session-state server and Microsoft SQL server.  Compression can be enabled by setting the compressionEnable to true in the web.config file. In this example, the session-state data will be serialized/desterilized using System.IO.Compression.GZipStream.
<sessionState  mode="SqlServer"  sqlConnectionString="data source=DB;Initial Catalog=LudmalDB"  allowCustomSqlDatabase="true"  compressionEnabled="true"/>

With this compression feature, developers can often reduce the time it takes for a web application to respond by reducing the size of session data.

View State mode for Individual Controls

View state is a mechanism to maintain page controls’ state on subsequent post backs. ASP.NET stores the view state data for controls that are in the page, even if it’s not necessary. Since the view state data is stored in the pages’ html, the size of the request object will be increased, and make performance worse.

In ASP.NET 4.0, each web control will include a ViewStateMode property which lets developers disable view-state by default, and enable it just for the controls for which a persistence of state is required. ViewStateMode has the following three values;

  • Enabled – enables the view state for this control and any child control.
  • Disabled – disable the view state.
  • Inherits – this specify the control uses the settings from its parent control.

 

By setting these values in page controls accordingly, a significant performance improvement can be gained in response-time.

Page.MetaKeywords and Page.MetaDescription properties

To increase the relevance of pages in searches, developers should  include relevant “keyword” and “description” meta tags in the html <head> section.     Unfortunately, it takes some time to add these tags for each and every page, and the alternative of adding these tags programmatically was difficult.

But with ASP.NET 4.0, there are two new properties in the code behind file;
  • Page.MetaDescription – equivalent to meta name “description”
  • Page.MetaKeywords – equivalent to meta name “keywords”

This will enable developers to easily and programmatically add the relevant keywords and description.

 

This will even be useful for Master pages—where you only have to add these properties in the master page. In addition to “keywords” and “description” settings in the code behind, developers can also set these values within the @Page directive.This will even be useful for Master pages—where you only have to add these properties in the master page. In addition to “keywords” and “description” settings in the code behind, developers can also set these values within the @Page directive.

Response.RedirectPermanent
Method

ASP.NET 4.0 has improved SEO (Search-engine Optimization) facilities. Typically developers useResponse.Redirect(string url) to handle requests for old URLs. However, this leads to an extra round trip to access the old URLs and so will negatively affect your page-ranking  in search-engines.

ASP.NET 4.0 introduces a new Response.RedirectPermanent(string url) helper method to be used as HTTP 301 (Moved permanently) to handle requests. This will enable search-engines to index URLs and content efficiently and thus improve the page rankings.
 

Routing in ASP.NET

Routing will let developers serve meaningful URLs to users and map them with the actual physical files. This URL-rewriting mechanism enables developers to write high ranking, search-engine optimized web applications. For example, URL for a page which displays an actual product might look like the following;

By using routing the URL will look like the following


In this way, the URLs will be more easily remembered by users.  It will also significantly improve the search-engine page rankings of the web site.

The following example shows how to implement routing behavior in ASP.NET 4 using new MapPageRoute in Route class.

public class Global : System.Web.HttpApplication
{   void Application_Start(object sender, EventArgs e)   
     {    
          RouteTable.Routes.MapPageRoute("ProductsRoute",       "product/{prodId}", "~/products.aspx");      
     }
}


Increase the URL character length

In previous versions of ASP.NET,  URLs were limited to 260 characters in length. But in ASP.NET 4.0 developers have the option of increasing or decreasing  the length of URLs by using  the new maxRequestPathLength and maxQueryStringLength. I’ll illustrate this in an example.

<httpRuntime maxRequestPathLength="360" maxQueryStringLength="1024" />

In previous versions of ASP.NET you were limited to a fixed set of characters but in v4, developers can also validate the invalid characters by specifying values in the requestPathInvalidChars attribute.

 

More details at: http://www.simple-talk.com/dotnet/asp.net/asp.net-4.0-features/