Thursday, July 27, 2006

Patching code in an aspx.cs file, without rebuilding

You've just built, tested and gone live with a new build of your ASP.NET web site when you discover a bug in thispage.aspx.cs. Your next build/test/release isn't planned for a few weeks, but the bug needs to be fixed now, if not sooner. What to do?

If the bug was in thispage.aspx, you could simply fix it and apply the new page to the live site as a patch, knowing that it won't affect any other pages on the site. Since the bug is in the code-behind file, you can't do this without a full build and associated testing, etc.

Solution:

Copy the code from thispage.aspx.cs to thispage.aspx (put the code in a <script language=CS runat=server></script> tag), and make these changes to thispage.aspx:
  • Remove the namespace / class declaration
  • Remove any declarations of controls as variables
  • Change any "using" statements to <%@ Import ... %> statements
  • Remove "Codebehind" and "Inherits" attributes from <%@ Page %> tag
That's it (more or less). Test and deploy thispage.aspx as a patch to the live site.

Then for the next full release, fix the bug in thispage.aspx.cs and remove the above code from thispage.aspx.