Sunday, July 10, 2011

Converting a Web Site project to a Web Application project

I recently had to convert a Web Site project to a Web Application project and unfortunately, the entire process is largely manual even while using the latest release of Visual Studio--Visual Studio 2010 SP1.  There is an MSDN article which describes this process (http://msdn.microsoft.com/en-us/library/aa983476.aspx), but I have found numerous things missing from the documentation, therefore, I am outlining the steps I followed here:

  1. You will have to first create a blank or empty Web Application project for the target .Net Framework (such as .Net 3.5 or .Net 4.0 etc.)
  2. You can then add all of your existing items from your Web Site project to the newly created Web Application project
  3. If you have an App_Code folder in your current Web Site project, you will need to rename the folder to something such as CodeFolder or ClassFiles etc. due to this problem with the App_Code folder in Web Application projects as described here: http://vishaljoshi.blogspot.com/2009/07/appcode-folder-doesnt-work-with-web.html
  4. For any folders that have content contained within them, if there are any .aspx.cs or .cs files that will need to be included as part of the compilation, you should highlight all of the items and open the Properties menu.  For the Build Action property, set the Build Action to Compile.  For any .aspx or .ascx files, ensure that these files still have the default Build Action set to Content.
  5. If you have any missing project references, you will have to add the appropriate references through the Add Reference Dialog.
  6. There may be several instances where the original .aspx or .ascx files cause compilation errors that were completely missed/unnoticed in Web Site projects.  In those instances, you will have to manually correct those errors.
  7. Unfortunately, you may have to correct each error on a file-by-file basis while building/re-building between each change since Visual Studio will usually not be able to display all of the errors in the Web Application project simultaneously. 
  8. Since the creation of the designer files associates the partial classes with a namespace, you will have to use the "Surround With" option to surround all of your code behind partial classes with a namespace.
  9. Unfortunately, doing this also requires you to update all of your Page Directives for the Inherits attributes to match the corresponding namespace and class name for each and every one of your pages/controls as well.  If you are using a tool such as Resharper, Resharper should provide you with Intellisense to autocorrect the fully qualified name to include the namespace in the Page Directive.
  10. If you have an existing Global.asax file, you will need to copy the appropriate code from it and delete the existing Global.asax file.  You can then re-create it by adding a new Global Application Class.  This is required because there is no option to "Convert to Web Application" for a Global.asax file.
  11. For any .aspx or .ascx files you have in the project, highlight all of the items and select "Convert to Web Application".  This will generate the appropriate designer files for you.
  12. Re-build the project until all errors have disappeared.  If no errors display, you have successfully converted a Web Site project to a Web Application project!  Whew....

No comments:

Post a Comment