What is the difference between an ASP.NET Web Site and Web Application?
In this lesson I discuss the major differences between an ASP.NET Web Site and an ASP.NET Web Application. I’ll help you make a determination which type of project to choose by helping you understand the result that each choice offers.
The major differences include…
- How sites are deployed and maintained
- Personal (or team) coding style preference
Project Template Availability by Visual Studio Version
|Visual Studio Version||Web Application||Web Site|
|VS 2002 (.NET 1.0)||X||—|
|VS 2003 (.NET 1.1)||X||—|
|VS 2005 (.NET 2.0)||—||X|
|VS 2005 Service Pack 1 (.NET 2.0)||X||X|
|VS 2008 (.NET 2.0 – 3.5)||X||X|
When ASP.NET was first released the model for creating web sites was dramatically altered from the classic ASP model. Web Applications were the only project type available to .NET developers in .NET 1.x.
Included in the major changes to the .NET Framework when .NET 2.0 was released was the “new and improved” way of developing web sites. Visual Studio therefore supported Web Sites, but not Web Applications.
Because so many projects were already live and supporting Web Applications, many developers “requested” that Web Applications would continue to be supported in future versions of .NET. As of Visual Studio 2005 SP1, web applications were again supported.
How should I choose whether to use a Web Site or a Web Application?
Difference in Deployment and Maintenance
|ASP.NET Web Site||ASP.NET Web Application|
Difference in Development
|ASP.NET Web Site||ASP.NET Web Application|
More About Web Applications
Since Web Applications are treated like other .NET projects a project file determines which files should be compioled as part of the project. The language of the project must be determined when the project is created and each code file must used the selected language. This is because a single compiler is used to compile all code in the site into one .dll which is stored in the “bin” directory.
Since all compiled code resides in the same dll public functions in one class are visible to the code in another class. This allows developers to call page specific functions from other pages.
Changes to a single code file requires a full re-compile of the Web Application. The .dll in the bin folder can then be uploaded. It requires careful planning to ensure new bugs aren’t introduced to the production site when uploading bug fixes or other changes.
More About Web Sites
Web Sites are not treated like other .NET projects. They are not managed by a project file, rather Visual Studio looks at a folder to see which files should be included (all files in the directory are included). This is mainly becuase of the way Web Site projects are compiled. The web.config file indicates to IIS that the site must be compiled. Each pages code file is compiled into its own dll at runtime (i.e. Default.aspx.dll, Login.aspx.dll) and is merged with the associated .aspx page.
Because each page is compiled into its own dll, different compilers may be used (i.e. Default.aspx can use C# while Login.aspx uses VB.NET). However, since each page becomes its own dll, the pages don’t have references to each other and can therefore not call functions in another page (even public functions).
To allow developers to create utility classes that have shared code .NET introduced a special folder where shared code is stored (the App_Code folder). All code that is placed into the App_Code folder is compiled into a .dll at run-time which is copied to the bin folder. With the dll in the bin folder it is automatically referenced by all pages making the public classes and functions available to all pages. Page specific functions may be placed in partial classes in the App_Code folder to achieve the same effect as public page functions in Web Applications.
The App_Code folder determines which compiler to use based on the first code file it finds. This means, by default, only one language may be used in the App_Code folder. Developers may create sub-directories in the App_Code folder for [different languages|Adding CS and VB to the App_Code Folder] if desired. Each of these sub-directories must be registered with the web.config file to indicate which compiler should be used (C# or VB.NET). If multiple languages are used, one dll will be generated for each language.
Changes to individual code files are easily deployed to the production web site. Only the changed file needs to be deployed. If the file is a page code file, only that one page will be re-compiled the next time it is requested. If the file is in the App_Code folder only the App_Code dll will be re-compiled. If the web.config file is changed, the entire pre-compiled web site cache is cleared from the server and the whole site is re-compiled.
So does it really make a difference which project type I choose?
Whether you choose to use a Web Application or a Web Site as your project type will have no bearing on the experience that the end user of your site will have. This choice will only affect your development and deployment experiences.
Typically a Web Application is a good choice if you are developing a website to be deployed similar to a software application. You may be developing a web site package that, once deployed you will not directly maintain. Updates to the site may also be handled through an installer which will either update a portion of the site or overwrite the entire site with a newer version.
Web Site projects are a good choice for sites over which you will have full control. If you are the developer of a custom site which requires constant updates and programming changes, you may want a project that allows you to deploy individual code files. This also requires careful planning. If you upload only a single file but the changes to that file are dependent on other changes, your changes will most likely result in run-time errors.
Leave a Reply