Unrecognized attribute ‘targetFramework’

I just installed the 4.0 framework on two servers and then had VS 2010 upgrade a test site from 3.5 to 4.0. When I tried accessing the site in my browser I got the error message Unrecognized attribute 'targetFramework'. The solution appears to be simple, just right-click the site in IIS, switch to the ASP.Net tab and change the framework to the 4.0 framework. Unfortunately for me our servers are 64-bit but are running IIS in 32-bit compatibility mode (Enable32bitAppOnWin64) because of some 3rd-party COM components. And because of this the ASP.Net tab apparently doesn’t show up in IIS. (And in fact the 32-bit version of AspNetMmcExt.tlb which controls that tab doesn’t even get installed which makes a little sense I guess.)

So without the ASP.Net tab how do you set the site’s framework version? Pretty easily, luckily.

First you need to find out your site’s IIS ID. Launch IIS Manager, click on Web Sites and look for your site’s Identifier in the right-hand panel. In my case its “1966841543”.

Next, drop to a command-line and navigate to the directory of the framework version that you want to use, in my case C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319

Finally, run the following command substituting your site’s ID as needed:
aspnet_regiis.exe -s W3SVC/1966841543/ROOT

In my case I was only testing a sub-site so I actually ran:
aspnet_regiis.exe -s W3SVC/1966841543/ROOT/V2