{"id":866,"date":"2010-04-22T00:00:00","date_gmt":"2010-04-22T00:00:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/asp-net-4-0-features\/"},"modified":"2021-05-17T18:35:03","modified_gmt":"2021-05-17T18:35:03","slug":"asp-net-4-0-features","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/development\/dotnet-development\/asp-net-4-0-features\/","title":{"rendered":"ASP.NET 4.0 Features"},"content":{"rendered":"<div id=\"pretty\">\n<p class=\"start\">The focus of Microsoft&#8217;s latest <b>ASP.NET 4<\/b>has mainly been on improving the performance and Search-engine Optimization (SEO). In this article, I&#8217;ll be taking a look at what I think are the most important new features in ASP.NET 4.<\/p>\n<ul>\n<li>Output cache extensibility  <\/li>\n<li>Session state compression  <\/li>\n<li>View state mode for individual control  <\/li>\n<li><i>Page.MetaKeyword<\/i> and <i>Page.MetaDescription<\/i> properties  <\/li>\n<li><i>Response.RedirectPermanent<\/i> method  <\/li>\n<li>Routing in ASP.NET  <\/li>\n<li>Increase the URL character length  <\/li>\n<li>New syntax for Html Encode  <\/li>\n<li>Predictable Client IDs  <\/li>\n<li><i>Web.config<\/i> file refactoring  <\/li>\n<li>Auto-Start ASP.NET applications  <\/li>\n<li>Improvements on Microsoft Ajax Library<\/li>\n<\/ul>\n<p>I&#8217;ll describe the details of each of these features in the following sections.<\/p>\n<h2>Output Cache extensibility<\/h2>\n<p>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. <\/p>\n<p>But with ASP.NET 4.0 developers can extend their caching by using <b>Output-cache providers<\/b>. Developers can now create &#8216;output-cache providers&#8217; that store the cache contents to any persistence mechanism such as databases, disks, cloud storage and distributed cache engines.<\/p>\n<p>To create a custom output-cache provider, a class which derived from <b>System.Web.Caching.OutputCacheProvider<\/b> 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 <b>web.config<\/b> file as shown in the following screen capture.<\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1012-LS1.JPG\" alt=\"1012-LS1.JPG\" \/><\/p>\n<p>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.<\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1012-LS2.JPG\" alt=\"1012-LS2.JPG\" \/><\/p>\n<p>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 <b>GetOutputCacheProviderName()<\/b> 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.<\/p>\n<h2>Session State compression<\/h2>\n<p>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&#8217;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.<\/p>\n<p>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.&#160; Compression can be enabled by setting the <b>compressionEnable <\/b>to true in the <b>web.config<\/b> file. In this example, the session-state data will be serialized\/desterilized using <b>System.IO.Compression.GZipStream.<\/b><\/p>\n<pre class=\"lang:c# theme:vs2012\">&lt;sessionState&#160; mode=\"SqlServer\"&#160; sqlConnectionString=\"data source=DB;Initial Catalog=LudmalDB\"&#160; allowCustomSqlDatabase=\"true\"&#160; compressionEnabled=\"true\"\/&gt;<\/pre>\n<p>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. <\/p>\n<h2>View State mode for Individual Controls<\/h2>\n<p>View state is a mechanism to maintain page controls&#8217; state on subsequent post backs. ASP.NET stores the view state data for controls that are in the page, even if it&#8217;s not necessary. Since the view state data is stored in the pages&#8217; html, the size of the request object will be increased, and make performance worse.<\/p>\n<p>In ASP.NET 4.0, each web control will include a <b>ViewStateMode <\/b>property which lets developers disable view-state by default, and enable it just for the controls for which a persistence of state is required. <i>ViewStateMode<\/i> has the following three values;<\/p>\n<ul>\n<li>Enabled &#8211; enables the view state for this control and any child control.  <\/li>\n<li>Disabled &#8211; disable the view state.  <\/li>\n<li>Inherits &#8211; this specify the control uses the settings from its parent control.<\/li>\n<\/ul>\n<p>By setting these values in page controls accordingly, a significant performance improvement can be gained in response-time.<\/p>\n<h2><i>Page.MetaKeywords<\/i> and <i>Page.MetaDescription<\/i> properties<\/h2>\n<p>To increase the relevance of pages in searches, developers should &#160;include relevant &#8220;keyword&#8221; and &#8220;description&#8221; meta tags in the html &lt;head&gt; section.&#160;&#160;&#160;&#160; Unfortunately, it takes some time to add these tags for each and every page, and the alternative of adding these tags programmatically was difficult.<\/p>\n<p>But with ASP.NET 4.0, there are two new properties in the code behind file;<\/p>\n<ul>\n<li><i>Page.MetaDescription<\/i> &#8211; equivalent to meta name &#8220;description&#8221;  <\/li>\n<li><i>Page.MetaKeywords<\/i> &#8211; equivalent to meta name &#8220;keywords&#8221;<\/li>\n<\/ul>\n<p>This will enable developers to easily and programmatically add the relevant keywords and description.<\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1012-LS4.JPG\" alt=\"1012-LS4.JPG\" \/><\/p>\n<p>This will even be useful for Master pages-where you only have to add these properties in the master page. In addition to &#8220;keywords&#8221; and &#8220;description&#8221; settings in the code behind, developers can also set these values within the @Page directive. <\/p>\n<h2><i>Response.RedirectPermanent<\/i> Method<\/h2>\n<p>ASP.NET 4.0 has improved SEO (Search-engine Optimization) facilities. Typically developers use <b>Response.Redirect(string url<\/b><i>)<\/i> 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&#160; in search-engines.<\/p>\n<p>ASP.NET 4.0 introduces a new <b>Response.RedirectPermanent(string url)<\/b> 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.&#160; <\/p>\n<h2>Routing in ASP.NET <\/h2>\n<p>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;<\/p>\n<pre class=\"lang:c# theme:vs2012\">http:\/\/www.ludmal.net\/showproducts.aspx?prodId=24 <\/pre>\n<p>By using routing the URL will look like the following<\/p>\n<pre class=\"lang:c# theme:vs2012\">http:\/\/www.ludmal.net\/products\/ipod <\/pre>\n<p>In this way, the URLs will be more easily remembered by users. &#160;It will also significantly improve the search-engine page rankings of the web site.<\/p>\n<p>The following example shows how to implement routing behavior in ASP.NET 4 using new <i>MapPageRoute<\/i> in <i>Route<\/i> class.<\/p>\n<pre class=\"lang:c# theme:vs2012\">public class Global : System.Web.HttpApplication { &#160;&#160;void Application_Start(object sender, EventArgs e) &#160;&#160;{ &#160;&#160;&#160;&#160;RouteTable.Routes.MapPageRoute(\"ProductsRoute\", &#160;&#160;&#160;&#160;&#160;&#160;\"product\/{prodId}\", \"~\/products.aspx\"); &#160;&#160;&#160;&#160;&#160;&#160;} } <\/pre>\n<h2>Increase the URL character length<\/h2>\n<p>In previous versions of ASP.NET, &#160;URLs were limited to 260 characters in length. But in ASP.NET 4.0 developers have the option of increasing or decreasing &#160;the length of URLs by using &#160;the new <b>maxRequestPathLength<\/b> and <b>maxQueryStringLength<\/b>. I&#8217;ll illustrate this in an example.<\/p>\n<pre class=\"lang:xhtml theme:github\">&lt;httpRuntime maxRequestPathLength=\"360\" maxQueryStringLength=\"1024\" \/&gt; <\/pre>\n<p>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 <b>requestPathInvalidChars<\/b> attribute.<\/p>\n<h2>New syntax for Html Encode<\/h2>\n<p>Html Encode method encodes a particular string to be displayed in a browser. It is important to encode strings prior it&#8217;s rendering in the page, mainly to avoid cross-site script injection (XSS) and HTML injection attacks. However, developers so often forget to call the encode function.<\/p>\n<p>In previous .NET versions, <b>Server.HtmlEncode()<\/b> or <b>HttpUtility.Encode()<\/b> methods has been used for string encoding as shown in the following example.<\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1012-LS5.JPG\" alt=\"1012-LS5.JPG\" \/><\/p>\n<p>ASP.NET 4.0 introduced new code expression syntax for encoding a particular string.&#160; While the syntax will render the output it also encodes the relevant string as shown below. Note &#8220;<b>:<\/b>&#8221; character after opening tag (&#8220;&lt;%&#8221;).<\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1012-LS6.JPG\" alt=\"1012-LS6.JPG\" \/><\/p>\n<p>The new encoding syntax provides an easy and concise way of encoding a particular string.<\/p>\n<h2>Predictable Client IDs<\/h2>\n<p>ASP.NET 4 now supports a new <b>ClientIDMode<\/b> property for server control. This property indicates how the Client ID should be generated to a particular control when they render. Client ID has been an important property of the server controls recently-especially with the success of jQuery and other Ajax scripting technologies.&#160; The ClientIDMode property has four values;<\/p>\n<ul>\n<li><b>AutoID<\/b> &#8211; This renders the output as it was before (example: <i>ctl00_ContentPlaceholder1_ListView1_ctrl0_Label1<\/i>)  <\/li>\n<li><b>Predictable (Default)<\/b>&#8211; Trims all &#8220;ctl00&#8221; strings in the Client Id property.  <\/li>\n<li><b>Static<\/b> &#8211; Full control over the Client ID (developer can set the Client Id and it will not be changed after the control renders)  <\/li>\n<li><b>Inherit<\/b> &#8211; Allow control to inherit the behavior from its parent control<\/li>\n<\/ul>\n<p>Client ID property can be set in three different ways;<\/p>\n<ul>\n<li>Directly on individual control  <\/li>\n<li>On the container control. (All the child controls will inherit the settings from parent\/container control)  <\/li>\n<li>Page or User Control level using &lt;%@ Page%&gt;&#160; or &lt;%@ Control %&gt; directives.  <\/li>\n<li>Directly in the <i>web.config<\/i> file. All the controls within the web application will inherit the settings.<\/li>\n<\/ul>\n<p>New <b>ClientIDRowSuffix<\/b> property on databound controls also gives a similar functionality when rendering an each data item. Once you set the relevant databound property to ClientIDRowSuffix, the value will be added as a suffix to individual row elements. <\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1012-LS7.JPG\" alt=\"1012-LS7.JPG\" \/><\/p>\n<p>After the control renders the &#8220;State&#8221; value will be added as a suffix to each data row element.<\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1012-LS8.JPG\" alt=\"1012-LS8.JPG\" \/><\/p>\n<h2>Web.config refactoring<\/h2>\n<\/p>\n<p>Over the past few years <b>web.config<\/b> file has grown significantly as ASP.NET has used it for more and more features such as routing, Ajax, IIS 7 and version compatibility. This has made it trickier to maintain even with the Visual Studio environment.<\/p>\n<p>With ASP.NET 4, most of the major elements have been moved to the <b>machine.config<\/b> file. This has enabled developers to maintain a cleaner, less cluttered, <i>web.config<\/i> file. The new <b>web.config<\/b> file is either empty, or includes just the .NET framework version details as shown in the following example.<\/p>\n<pre class=\"lang:xhtml theme:github\">&lt;?xml version=\"1.0\"?&gt;&#160; &lt;configuration&gt;&#160;&#160; &lt;system.web&gt;&#160;&#160;&#160; &lt;compilation targetFramework=\"4.0\" \/&gt; &#160;&#160;&#160;&lt;\/system.web&gt;&#160; &lt;\/configuration&gt;<\/pre>\n<h2>Auto-Start ASP.NET Applications<\/h2>\n<p>Most application requires initial data load or caching operations to be done before serving the client requests. Typical this happens only when the first user request a page. However, often developers and web administrators write fake requests to keep the application alive to increase the response time. To overcome this issue, ASP.NET 4 introduce new Auto-Start feature. Auto-start feature available with IIS 7.5 and it initialize the ASP.NET application to accept requests.<\/p>\n<p>To configure the Auto-start, you need to configure the &#8220;Application pool&#8221; worker process by setting the startMode attribute to &#8220;AlwaysRunning&#8221; in the <i>applicationHost.config <\/i>file. (C:\\Windows\\System32\\inetsrv\\config\\applicationHost.config)<\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1012-LS9.JPG\" alt=\"1012-LS9.JPG\" \/><\/p>\n<p>As soon you save the <i>applicationHost.config <\/i>file the worker process will start and initialize the required application operations before the first user has been served. <\/p>\n<h2>Improvements on Microsoft Ajax Library<\/h2>\n<p>Microsoft Ajax library is client side library which includes high performance server -based user controls and asynchronous page rendering controls. Ajax Library enables developers to easily and quickly write responsive database-driven applications. <\/p>\n<p>There are some significant improvements in the Ajax Library in the ASP.NET 4. of which the most important seem to be&#8230;<\/p>\n<ul>\n<li><b>Scrip Loader<\/b> &#8211; the new script loader control enable developers to load all the required scripts only once, thereby eliminating the unnecessary subsequent requests to the server. It supports the &#8216;lazy load&#8217; pattern which loads scripts only when necessary, and loads scripts in combination, in order to increase the performance of loading a page. It also supports the <i>jQuery<\/i> script and custom scripts.  <\/li>\n<li><b><i>JQuery<\/i> Integration<\/b> &#8211; <i>JQuery<\/i> is very popular third party javascript library. ASP.NET 4 extensively supports the integration for <i>jQuery<\/i> by mixing the <i>jQuery<\/i> and Ajax plug-ins seamlessly.  <\/li>\n<li><b>Client Data Access<\/b> &#8211; by using pre-defined client controls inside the Ajax Library, developers can easily build asynchronous data-driven applications. For example client <b>DataView <\/b>control will display one or more records by consuming a WCF service. All the relevant time-consuming operations will be handled by the Ajax library asynchronously.<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>ASP.NET 4 includes plethora of new features which will enable developers to write high performance, search-engine friendly web application quickly. The features I&#8217;ve mentioned seem to be the most important of all the new features in ASP.NET 4. By upgrading your existing web applications to up-coming ASP.NET 4, you are likely to see an improvement in performance and search-engine optimization.<\/p>\n<h3>More readings:<\/h3>\n<p><a href=\"http:\/\/www.asp.net\/\">http:\/\/www.asp.net\/<\/a><\/p>\n<p><a href=\"http:\/\/www.asp.net\/learn\/aspnet-4-quick-hit-videos\/\">http:\/\/www.asp.net\/learn\/aspnet-4-quick-hit-videos\/<\/a><\/p>\n<p><a href=\"http:\/\/weblogs.asp.net\/scottgu\/archive\/2009\/08\/25\/vs-2010-and-net-4-series.aspx\">http:\/\/weblogs.asp.net\/scottgu\/archive\/2009\/08\/25\/vs-2010-and-net-4-series.aspx<\/a><\/p>\n<p><a href=\"http:\/\/www.hanselman.com\/blog\/ASP4WhirlwindTourAroundNET4AndVisualStudio2010Beta1.aspx\">http:\/\/www.hanselman.com\/blog\/ASP4WhirlwindTourAroundNET4AndVisualStudio2010Beta1.aspx<\/a><\/p>\n<\/div>\n<p><b><\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>\n","protected":false},"author":122076,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143538],"tags":[4143,4156,4157,5172,5173],"coauthors":[],"class_list":["post-866","post","type-post","status-publish","format-standard","hentry","category-dotnet-development","tag-net","tag-asp","tag-asp-net","tag-asp-net-4","tag-ludmal-de-silva"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/866","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/122076"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=866"}],"version-history":[{"count":6,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/866\/revisions"}],"predecessor-version":[{"id":91076,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/866\/revisions\/91076"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=866"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}