Wednesday, January 6, 2010

ASP.NET MVC Action methods called twice

I'm converting an ASP.NET Web Forms application to ASP.NET MVC. The UI is not changing, so I'm copying and pasting chunks of ASPX and ASCX files into my Views, and then replacing the Web Forms-specific code (example: tags with 'asp' prefixes) with HTML and/or calls to the System.Web.Mvc.HtmlHelper class. But this conversion doesn't happen instantaneously - there might be a period of time between the creation of the view and completion of conversion.

I had a breakpoint set in an action method and noticed that the method was being called twice. Once with the parameter that I expected (an int representing a primary key in a database) but then also with the default value specified when the route was registered. The net effect was that the desired View was displayed, but that also a call was made to the previous View. Not good.

I spent time with Fiddler, and I got an idea. I replaced all of the remaining legacy asp tags that I hadn't yet converted, and the problem disappeared. Sure, sooner or later the problem would have resolved itself as the conversion process progressed, but I'm (unfortunately?) not the type of person who can let a mystery like this one remain unsolved.