ASP.NET Web Site or Web Application?

Downloads
Lessons
FreeDelivery234x60static
Filofax, it's a way of life

Free Book Offer - White - 300x250
Ready to surf the web?

What Icon 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

Brief History

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 Icon 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
  • Site does not have to be pre-compiled
  • Deploy both .aspx file and associated code file (.cs or .vb)
  • Small changes to the site do not require a full re-deployment (i.e. you can upload only the code file that was changed)
  • Site has to be pre-compiled prior to deployment
  • Deploy .aspx page, but not associated code file (the pre-compiled dll will be uploaded)
  • Even small changes require a full re-compile of the entire site (i.e. if the code for a single page changes the whole site must be compiled)

Difference in Development


ASP.NET Web Site ASP.NET Web Application
  • Mixing programming languages is allowed (code for one page can be in C# and another page can be coded in VB.NET)
  • Public functions on a page are not visible to code in other pages
  • Utility classes / functions must be placed in a special ASP.NET folder (the App_Code folder)
  • Web Sites do not have a .csproj/.vbproj file that manages the project (the folder that contains the site becomes the project root)
  • Only one programming language allowed per site (decide on programming language when starting project)
  • Public functions on one page are visible to code in other pages
  • Utility classes / function can be placed anywhere in the applications folder structure
  • Web Applications are treated like other .NET projects and are managed by a project file (.csproj or .vbproj)

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 runtime 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 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.


MAGIX Music Maker

So What Icon 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 seiiling 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 dependant on other changes, your changes will most likely result in runtime errors.



blog comments powered by Disqus