Visual Studio Web Projects vs Web Sites

Visual Studio 2003 which supported the Microsoft .NET Framework 1.0 and 1.1 provided a method of creating web applications called Web Application Projects.   When Microsoft released Visual Studio 2005 it only supported the creation of web sites; web application projects were no longer an option.   Microsoft then released an add-on to bring back web application projects (in a new form) after numerous complaints from users about difficulties migrating from Visual Studio 2003 web projects to Visual Studio 2005 web sites.  Initially the support for web projects were scheduled to end concurrently with the end of life date of Visual Studio 2005.   Microsoft has elected to keep web projects around with Visual Studio 2008 so the debate between using web projects or web sites continues.

So what is the best practice for the creation of new web applications; use web projects or web sites?   The short answer is it depends.  Each option presents a different way of writing, managing and compiling the code but the end product is still a .NET web application that can be deployed on any ASP.NET compatible web server.

Before we dive in too deep there is once concept that needs mentioning.  Both web sites and web projects in Visual Studio 2005/2008 utilize a new concept in .NET 2.0 called partial classes.   Partial classes allow the developer to split a single .NET class across multiple code files.   This is very different from Visual Studio 2003 where all of the code for a class was managed in a single file.

Below is an overview of each option.  Hopefully this will provide you with enough information to allow you to make an educated decision on which one will work best for you.

Web Application Projects
Web Projects are created by selecting the file menu and then choosing project and then under your language of choice in the new projects window go to the web folder and select ASP.NET Web Application.   With web application projects there is a .proj file that maintains a list of all files, references and project settings used in that project.

This project type allow developers to maintain separation between the presentation and logic of a web page by using an .aspx page and two partial class files.  In this situation the .aspx page contains HTML and .NET markup for creating the visual elements and controls.   A partial class file with the extension of designer.cs or designer.vb is used to hold the control declarations and a .cs or .vb partial class is used for code related to events.  It is important to understand that Visual Studio manages the .designer.cs files and that developers should never manually modify them.

When a web application project is built the code files are compiled into a single assembly file and placed in the web’s BIN directory.   It is important to understand that at no time is the aspx files compiled when using a web application project.  Mismatched control names between the .aspx and designer code file will not be identified during the build process.  Only during application testing will these types of errors surface.   The odds of getting a mismatch between a control name in the .aspx and the declaration in the designer code file is slim if you do not manually change the designer file.

Deploying a web application project is as simple as using xcopy to move the complete application to the production environment.   Since all of the code-behind files (.cs or .vb) is compiled into the assembly it is only necessary to copy the .ASPX and .DLL files.  It is also possible to use the Visual Studio publish feature to make copying the needed files to the final location simple.

One benefit that web application projects have over the web site type is that developers can utilize the pre-build and post-build events built into Visual Studio.   Since a web site doesn’t have a project file it is not possible to set up these events within Visual Studio.   Another benefit is the ability to specify the default namespace used by the application.

Web Sites
Web sites which were introduced in Visual Studio 2005 can be created by selecting the file menu and then choosing new and then web site.   You can create new web sites on the file system, local IIS server or other locations.   If you choose to use the file system, Visual Studio will use a built in light weight web server for testing and debugging your application.   It is important to understand that there is not a project file for web sites.   This means that all files in your web site folders are included in the solution.

An aspx page in a web site is a partial class that correlates with a .cs or .vb (aka code-beside) file that contains the rest of the code for the class. The .aspx file contains HTML and .NET markup that defines the visual elements and controls on the page.  The .cs or .vb code-beside files contains the event handlers for the controls.   When a build occurs for a web site both the aspx and code-beside file are compiled.   This is very different from a web application project in which only the code files are compiled.  Having both the .aspx and code-beside files compiled help the developer identify certain types of errors during the build process and prior to actual testing.

Deploying a web site is as simple as using xcopy to move all of the files including the aspx and code-beside files to the IIS server.   The web server will automatically compile the aspx and code-beside files into separate assembly files upon first access.

In some cases you may not want any of your code easily visible on your server.   In that situation you can use the Visual Studio publish option and remove the check box from the allow this precompiled site to be updatable. This will cause Visual Studio to create an assembly with all of the web site code (including the controls declared in the aspx files).  This leaves a single assembly and .aspx placeholder files which are pretty much empty.   This in essence makes it more difficult (but not impossible) to directly view or update the code for the web site.

If you deploy your web site using the xcopy method it is possible to make quick fixes or updates to your web site just by copying the updated files (not the whole application) to the IIS server.   In most cases this can be done without requiring any down time or restarting of the IIS server.    If you are replacing a partial class on the IIS server that is referenced by other web site classes you may experience problems unless you restart the web server (ex.  IISRESET), so this isn’t always a “no down time” type of update.

Web sites was Microsoft’s intended direction for developing web applications starting with Visual Studio 2005.   Microsoft released an update to Visual Studio 2005 to add back web application projects due to upgrade/migration issues from Visual Studio 2003 web application projects to Visual Studio 2005 web sites.   Both types allows developers to create web applications and deploy them in a similar manner

So should you use web sites or web application projects?  Only your architect or lead developer can make that decision. The important thing is to keep consistent with your development practices.

Leave a Reply