Home > ASP.NET > Converting ASP.NET Web Site to Web App

Converting ASP.NET Web Site to Web App


There is a common mistake which almost most of us made when we  created a new web site with Visual Studio 2005. The earlier version of Visual Studio 2005 does not include a template for web application. In other words, it was only possible to create website.

So why do you have to convert from website to web app first of all? Well there are quite a few reasons to list out:

  Web Site Web App
Compilation of Markup Dynamic Dynamic
Compilation of code behind dynamic by default; can pre -compile the site using 2 modes – batch mode (one assembly per folder) and fixed-names (one assembly for each page or user control; this may result in increased memory usage) Precompiled; All code is compiled in to single assembly
Scope Only code in App_Code is available to all classes; namespaces are not created by default Since it is precompiled all page classes are in same assembly and are visible to all; namespace is created by default
File Structure Just aggregates files in a directory so based on file system; project file list and other assembly list are present in web.config (which violates SoC) has a neat project file which helps to have information of files and assemblies, helps in controlled build and deployment

After reading the above difference website option may look evil; but that’s not the case. There are cases when website may come in to rescue. In fact we have been using web site for our application for a long time. And deployment was not a problem, we used NANT to compile the application (batch-mode) and used INSTALLSHIELD to deploy the application.

So when to use website or webapp project? There is an excellent article from MSDN:

 

Task Web application projects Web site projects
Need to migrate large Visual Studio .NET 2003 applications X  
Prefer single-page code model to code-behind model   X
Prefer dynamic compilation and working on pages without building entire site on each page view (that is, save file and then simply refresh the page in the browser).   X
Need to control names of output assemblies X  
Need to generate one assembly for each page   X
Need stand-alone classes to reference page and user control classes X  
Need to build a Web application using multiple Web projects X  
Need to add pre-build and post-build steps during compilation X  
Want to open and edit any directory as a Web project without creating a project file   X

But after an over all comparison, Web Application looks more structured and gives us more control over the project.

How to convert a web site to web application?

ScotGu has given an excellent step-by-step approach to convert a web site to web application. (In fact the Microsoft consultant who came to audit our project also gave the same printout as guidance). This is an excellent resource to start with.

While I did the conversion I took notice of some points that are worth mentioning in addition to Scottgu’s list.

Pre-Requisites:

  1. Visual Studio 2005 does not have an option for web application project. The web application project template was later added to the visual studio in Visual Studio 2005 SP1.Please read here for What is new in Visual Studio 2005 SP1? Web application projects is one among them.
  2. If you are installing VS 2005 SP1 on Windows 2003 server you may get a weird error "The installation source for this product is not available". You must install a hotfix to fix this.
  3. So install the hot fix (point 2), restart the machine and then install VS 2005 SP1.
    Also see ScottGu’s suggestions on installing the service pack.

Steps in conversion:

  1. Create a web application project.
  2. Add reference to all of your dlls.
  3. Copy the files to the folder except web.config file
  4. Remember there is no App_Code in web application. So any classes in the app_code will not be visible to other classes in the project. This will break the build. To avoid this as a quick and effective fix, add a namespace to your classes in App_Code. And replace this class name with the fully qualified class name (with the newly added namespace) through out the project. (Of course not in the original class file in app_code). Renamed the app_code folder to something like Shared_Classes or something; this would avoid confusion.
  5. Change the Build Action to <compile> for all the classes in App_Code.
  6. Convert to Web Application.
  7. image
  8. When the convert to web application is being done the conversion may fail for some aspx files that use user controls that reside in the same directory and the same user control is also registered in the web.config file. Actually the creation of designer file may fail because of this error: tag has already been registered. To avoid this: register the user control in the problematic ascx page but choose a different TagPrefix (different from the one you have already registered in web.config file). For example:

These are few additional things I noticed while converting the project from web application to web site. Hope this helps!

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: