Home / Programming / Upgrading ASP.NET MVC 1.0 to 2.0 Gotchas

Upgrading ASP.NET MVC 1.0 to 2.0 Gotchas

I know ASP.NET MVC is on version 4 now… and version 1 is soooooo old news…. but our company finally got around to upgrading some old applications from ASP.NET MVC 1, and I thought I’d share some of the troubles we ran into. Admittedly, many of the issues were due to us using poor coding practices that the ASP.NET MVC team decided to stop supporting, but poor coding practices are rampant, so you may run into a few of these issues as well.

1. JsonResult no longer supports GET requests by default. This is easy to overcome, just by setting JsonRequestBehavior to AllowGet in all your responses.

2. HtmlHelpers no longer return strings, but return MvcHtmlStrings. For most people, this shouldn’t be a problem. But we had one application that made extensive use of HtmlHelpers to get around the lack of templates in MVC 1.0, and all of these needed to be re-written to return MvcHtmlStrings instead. The main problem is that you can get your app to compile, but if you have any views that reference these helpers and expect them to return strings…. expect errors from QA.

3. I’m murky on this part, but there’s an extra ModelBinding step that is less forgiving with properties that may throw exceptions. In one of our views we had a model with nullable properties. MVC 1 seems to only bind to the model after it is initialized, but MVC will try to get the properties on the model earlier. If there are properties that must be initialized before the getter is called, expect exceptions to be thrown. GetValueOrDefault() is your friend here.

4. MVC 2 is less forgiving of HTML violations in ID names. We had some fields where the id contained pipes or other invalid characters. Most browsers are quite forgiving of this, but the Html.TextBox() method in MVC2 will “helpfully” replace pipes or other invalid characters with underscores. This can be quite a surprise to any javascript referencing these fields.