The ASP.NET team at Microsoft often appears to be on a rampage to get new versions of cool products out the door, and ASP.NET MVC could be their frequent-release poster child. No sooner had ASP.NET MVC 2 hit the download pages and into the Visual Studio 2010 installation package than Microsoft was already making a preview release of ASP.NET MVC 3 available far and wide. Or so it seemed at the time. In any case, less than a year after MVC 2 came, we're getting close to a final release of MVC 3.
This article is derived from some upcoming ASP.NET MVC courseware that I'm developing and writing for AppDev.
This frenetic pace for releases can cause havoc with development teams, who often have to carefully retest their code to see what breaks with the new versions of tools. This is particularly true for out-of-band releases like MVC 3, which, unlike MVC 2, wasn't tied to a major release or update of Visual Studio or the .NET Framework. The good news with MVC 3 is that, other than one breaking change (that we know about, anyway), MVC 3 is an add-on to MVC 2 that is fully backward compatible. All MVC 3 does is add features on top of MVC 2, so you should be able to move existing applications from MVC 2 to 3. The process isn't automated or simple, but it should keep working just fine. Assuming it worked before you made the transformation!
MVC 3 isn't quite the major release implied by a full increment of the version number, but it certainly has some features that make coding MVC applications far easier, along with some major new features that will change how you build those applications. The old saw about Microsoft finally getting a product right in version 3 doesn't really hold in this case—the original release of MVC 1 was pretty dang impressive and highly useable for development—but MVC has certainly come of age with this latest release. Other than its one major, game-changing feature—the new Razor view engine—most of the new features are refinements of old features or additional options that complement existing features. In some cases, this makes the existing feature more usable and sometimes even easier to understand.
The one potential downside to MVC 3 is that it requires that you use Visual Studio 2010—including the Express version—and .NET Framework 4.0. If you built an application using Visual Studio 2008 and .NET 3.5, you'll have to first upgrade to Visual Studio 2010 and .NET 4.0 before upgrading from MVC 2 to MVC 3, and that may deter you for a while. But if the application is already Visual Studio 2010 and .NET 4.0, we really don't see any reason not to make the switch to MVC 3.
Breaking Change: Exception Filters
The one known breaking issue in MVC 3 is that Microsoft changed the order in which exception filters execute. This probably won't be a major issue for most MVC applications, and is only an issue if they use explicit exception filters with the Order properties set to the same value. The only built-in exception filter is HandleError, which checks the HTTP status code to see if it is 500 (internal server error) in order to handle otherwise unhandled exceptions. But this change also affects any custom exception filters in an application.
In MVC 2 and before, if you had exception filters on the controller class that had the same value for the Order property as exception filters on any action methods, the controller filters executed before the action method filters. In MVC 3, the action method exception filters now execute before any controller exception filters with the same Order value.
This change certainly makes sense, since it means that the more specific action method filter (one with a smaller scope) will execute before a more general controller filter. It's anybody's guess why this execution order wasn't part of the original MVC design, but I long ago learned that asking "why?" questions about Microsoft rarely yields satisfactory answers.
Anyway, the fix should be relatively easy if you still want the controller exception filters to execute before the action method filters. Just set or change the value of the Order property on the affected filters to reflect the order in which you want them to execute.
Upgrading an MVC 2 Project to MVC 3
You can install MVC 3 to run side-by-side with MVC 1 and MVC 2, so you can continue to build applications with older versions of MVC and maintain any existing applications while building new MVC 3 applications. Such is the beauty of the multi-version support in Visual Studio 2010. And when you're ready, you can upgrade applications using older versions of MVC to version 3 when you need to make use of the new features.
Unfortunately, the process is manual, unless you try Eilon Lipton's unsupported conversion tool (see below). It's not hard to do, but it does require changing a GUID, copying some files, changing web.config, and more. Sometimes I think it is better to just create a new MVC 3 project and copy stuff over as necessary, but on a large project that could be a lot of work.
The MVC 3 Release Notes list the basic steps (although it has a few details incorrect in the beta), and it is interesting and educational to convert at least one project manually. Just keep in mind that you'll have to convert any Visual Studio 2008 and .NET 3.5 projects to Visual Studio 2010 and .NET 4.0 first, before upgrading to MVC 3.
Although Microsoft doesn't make a conversion tool available, other people have. Eilon Lipton, a Microsoft employee on the MVC team, had a nice tool for upgrading MVC 1 applications to MVC 2, which he has updated for MVC 3 conversions. I've always found the tool to be reliable, but make sure you check the results in case an application has some weird condition that doesn't convert properly. Check it out on his blog. He doesn't post often, but it's usually good stuff.
There is a lot of cool stuff in MVC 3, and in the coming months I'll explore the best of it.
Don Kiely ([email protected]), MVP, MCSD, is a senior technology consultant, building custom applications and providing business and technology consulting services. His development work involves SQL Server, Visual Basic, C#, ASP.NET, and Microsoft Office.