Six Cool Things About ASP.NET 4.0

I’ve been using the Visual Studio 2010 and ASP.NET 4.0 betas and exploring some of the new features available in the new versions of VB 10 and C# 4.

I’ve put together a list of the top 6 things I like about the new releases:

1. User interface improvements

The Visual Studio 2010 user interface has been completely rewritten using WPF. It looks so much better than its predecessor. It’s also much easier to design Silverlight and WPF applications in the form designer.

2. Better ViewState Control

Although I’ve been developing an increasing number of new projects using ASP.NET MVC, I still maintain and create projects which use the traditional ASP.NET platform. I don’t have anything against ViewState (quite the opposite – it works) but it can make your pages pretty chunky and carry a lot of dead weight.

In ASP.NET 4.0, ViewState just got a lot more attractive. The introduction of the ViewStateMode property means you can turn viewstate on/off at control level even if it’s disabled at page level.

  • Enabled : Turns on the ViewState for the control.
  • Disabled : Turns off the ViewState for the control.
  • Inherit : Takes the value from the parent and set it accordingly.

In previous versions of ASP.NET, setting the EnableViewState property on a control would be ignored if the parent control (Page class) had ViewState disabled. This is great news as it allows developers to fine tune code and increase performance on .NET applications.

Read more about this feature on the freshly painted MSDN.

3. Custom client ID management

Referencing client Ids has been a bugbear or mine for a long time.  Make changes to your page or system structure and break references to HTML DOM objects in JavaScript.
With ASP.NET 4.0, there’s a nice little enumerator which allows you to set the ClientID of a control in one of four ways:

  • Legacy: Behave like ASP.NET 2.0
  • Static: It uses the same ID as the property.
  • Inherit: It uses the same ID as the parent.
  • Predictable: A new way of handling controls within databound controls (i.e. FormView which incidentally has had its rendering limitations removed in ASP.NET 4)

4. Native Support for URL Routing on Web Forms

Historically, I have used third part ISAPI filters to implement routes on ASP.NET projects. ASP.NET 4.0 lets you use the URL routing engine to map URLS to Web Forms.

Using MapPageRoute(), you can emulate MVC functionality by routing  “/dvd/the-shining” directly to a web form.  You can grab the data using Page.RouteData.Values like this (VB.NET):

Private Sub RegisterRoutes(ByVal routes As RouteCollection)
    Routes.MapPageRoute("dvd-detail", "dvd/{title}", “~/products/dvds.aspx")
End Sub
Private Sub Application_Start()
    RegisterRoutes(RouteTable.Routers)
End Sub

Getting the route data is as simple as:

Page.RouteData.Values("title")

5. Collection Initialisers in VB.NET  10

One issue with old-school VB.NET was that there was no way to declare and initialise a collection on one line.

You can now do this with the following syntax:

Dim post As New List(Of String) From {"Title", "Body"}

6. A bunch of other things

I wish I hadn’t limited this list to six items as there are so many new features I want to talk about.  For this reason, I’ve cheated and combined two of my favourite features into three categories; VS2010 features, C# 4.0 and VB 10.

Visual Studio 2010

C# 4.0

  • Optional parameters like VB.net
  • Better support for COM/JS

VB 10.0

  • Collection initializers
  • Anonymous methods