Agafonov Slava Digest about software

New features in ASP.NET 4

ASP.NET 4 introduces a number of features that improve core ASP.NET services such as output caching and session-state storage. Web Forms has been a core feature in ASP.NET since the release of ASP.NET 1.0. Many enhancements have been in this area for ASP.NET 4, including the following:

  • The ability to set meta tags.
  • More control over view state.
  • Easier ways to work with browser capabilities.
  • Support for using ASP.NET routing with Web Forms.
  • More control over generated IDs.
  • The ability to persist selected rows in data controls.
  • More control over rendered HTML in the FormView and ListView controls.
  • Filtering support for data source controls.
Html Encoded Code Expressions
Context: ASP.NET MVC 1.0 ships a great deal of HTML helpers used in the view templates to generate the HTML output. For example, <%= Html.TextBox("ProductName", Model.ProductName) %> where the first parameter is the Name/ID of the HTML element and the second one is its value. This produces the following output <input id="ProductName" name="ProductName" type="text" value="Product1" />.
Problem: One of the features asked by the developers is to have strongly typed HTML helpers that use lambda expressions to reference the model, and in this way, we can detect compile-time errors and have a better IntelliSense support.
Solution: ASP.NET 4 introduces a set of strongly-typed HTML helpers following the naming convention Html.HelperNameFor(). The list is enumerated:
  • Html.TextBoxFor()
  • Html.TextAreaFor()
  • Html.DropDownListFor()
  • Html.CheckboxFor()
  • Html.RadioButtonFor()
  • Html.ListBoxFor()
  • Html.PasswordFor()
  • Html.HiddenFor()
  • Html.LabelFor()
  • Html.EditorFor()
  • Html.DisplayFor()
  • Html.DisplayTextFor()
  • Html.ValidationMessageFor()
Implementation strategy: An example is shown in the Listing 25.
<%= Html.TextBoxFor(model=>model.ProductName) %>
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).

Compacting out-of-Process Session Data

I like using the Session object (I like the Cache object even better). Many developers object to the Session object because it imposes a footprint on the server even when a user isn't requesting a page. In ASP.NET 4 the sessionState tag in the web.config file now has a new attribute called compressionEnabled that causes serialized to data to be automatically compressed. Here's what it looks like:


Of course, Session data is only serialized if you're using a state server (where mode is set to "StateServer") or a database (mode="SqlServer"). But in those scenarios, this option will lower the impact of the user's footprint when storing data in the Session object -- provided you're willing to spend the CPU cycles to compact and un-compact your data.

Client IDs

Yes indeed. Now you can have your cake and eat it – almost. We give you control over the ClientIDs generated by server controls. ASP.NET does an excellent job of avoiding naming conflicts in the HTML document through the use of naming containers that guarantee uniqueness for a certain context. The result though, can be lengthy, complex and (importantly) difficult to predict ClientIDs.

This can make life a misery if you’re doing DOM manipulation in Javascript as you typically revert to some inline code to extract the ClientID. You’ll find <%= MyControl.ClientID %> littered all over the place. Given the surge in popularity of AJAX and client frameworks like jQuery, the pain caused by this “guarantee of safety” is becoming more acute.

Step forward the new ClientIDMode, an enumeration capable of taking on 4 values:

  • Legacy - Behave as ASP.NET 2.0 (this is set at the page level in machine.config)
  • Inherit (default) – Get the value from the parent
  • Static – Do exactly as I say. In other word, what you set as the ID becomes the ClientID. ASP.NET leaves it up to you.
  • Predictable – Particularly useful in databound controls, this can guarantee uniqueness but gives you control over how that uniqueness is achieved. eg it will append an incrementing value to the end of the generated ClientID or, if you’re using a GridView or ListView, you can append a DataSource field (such as ProductID for example)

Permanently Redirecting a Page

It would be wonderful if we never changed the URLs for our pages. However, in the real world that doesn't happen. Pages move around. Rather than let users get 404 errors when requesting a page using a URL that's worked in the past, ASP.NET programmers often put a Response.Redirect in the Page Load of the old page and have that Redirect send the user to the new location of the page.

In ASP.NET 4, you can now use the Response.RedirectPermanent method in the same way. RedirectPermanent issues HTTP 301 messages which signal to the client that this is a permanent redirection. Provided the client recognizes 301 messages, the client will automatically substitute the new URL for the old URL whenever the user requests the old URL, skipping the trip to the old location.

Alternatively, you could use Routing, which eliminates this problem. Implementing routing is much simpler in ASP.NET 4 and I'll cover that topic in a later column.

Control-Level Changes

Probably the most important change to existing controls is invisible: Many controls will be generating different HTML in ASP.NET 4. Partially this is a move to produce HTML that is more complaint with XHTML 4.0; partially it is an effort to produce HTML that CSS authors (i.e. not me) will find easier to style. Overall, there's a sharp reduction in the number of table tags being produced. However, if you have code that's dependent on the old HTML format, you can add this element to your config file to keep the old HTML:

<pages controlRenderingCompatibilityVersion="3.5"/>

New features in ASP.NET 4 Get Microsoft Silverlight

New features in Ajax 4.0:

AJAX 4.0 provides many flexible features - we will look at those features in this section. Here is a list of the enhancements made to the Preview 4 release of ASP.NET 4.0 for providing Ajax support:

  • Support for Live data binding
  • Support for Client-side template rendering
  • Support for declarative instantiation of client components
  • Support for using the Observer pattern on JavaScript objects and arrays
  • Support for invoking ADO.NET Data Services and Data Contexts
  • Support for the DataView control

Here's a general outline of a small part of the goodness:

  • New Shell, new File|New, new Extensions Manager
  • Lots of focus on "Code First" (TDD-friendly, etc)
  • Office and COM Interop that is actually fun to do.
  • Automatic Properties for VB, no more "_" for multi-line, inline subs, collection initializers
  • C# gets the dynamic keyword
  • F# is included out of the box
  • WPF Databinding in the Designer
  • Silverlight included out of the box
  • Piles of HTML Snippets for non-designer people
  • Better JavaScript Intellisense
  • MSBuild for C++
  • UML for VS Team Architecture
  • TFS Branch and Changeset visualizers
  • Parallel computing
  • Workflow speedup, new designer
  • SharePoint tooling


New features in ASP.NET 4 Get Microsoft Silverlight

Code Snippets

Code snippets are pre-developed code templates which can save time spent on thinking about the syntax. There are already a lot of built-in code snippets in VS2005 and VS2008. However, those are only available for the code-behind. Code snippets in VS2010 are introduced for JScript, HTML, and ASP.NET markup as well. In the screenshots below, we can see different snippet context menus for JScript and HTML.

Inside the script tag, it would be

New features in ASP.NET 4

And inside the HTML:

New features in ASP.NET 4


VS2010 will use MSDeploy to create web packages comprising your application, metadata, IIS settings, SQL Server schema and data, certificates and other dependencies. To read more about MSDeploy I’d suggest checking out the team blog.

One very cool deployment feature in VS2010 is web.config transformations. Each build configuration is given a unique transformation file to allow the web.config to be tweaked for different build configurations. eg  below I have just Debug and Release configurations. The Web.Release.config file contains a set of transformations that will be applied to the web.config during deployment. In my case I have 2 transforms, one replaces the connection string with my production connection string and the other removes the debug attribute from the compilation element thus ensuring my configuration is correct for production.

New features in ASP.NET 4 ASP4_4

If I add a new build configuration as below:

New features in ASP.NET 4 ASP4_5

I get a corresponding web.config transform file which I can opt to base on any existing transform file (or empty).

New features in ASP.NET 4 ASP4_6

New features in ASP.NET 4 Get Microsoft Silverlight



In this article, I've explained the new features of ASP.NET 4. Now, you can use the features in your own solution.

New features in ASP.NET 4

Comments (7) -

  • Agafonov Slava

    10/7/2010 11:57:21 PM | Reply

    I like new Search Engine Optimization features, also new features in ASP.NET 4 much more oriented on commercial web sites.

  • Alane Alaibilla

    11/23/2010 1:31:28 PM | Reply

    ASP.NET HTML formatting will help with SEO and get more traffic to a website

  • prachi

    1/24/2011 2:25:04 PM | Reply

    Great work dude, u gave nice post to us. Thanks for spending the time to discuss this, I feel strongly about it and love learning more on this topic.

Add comment


Copyright © 2018 - Design by FS