UGTS Document #77 - Last Modified: 8/29/2015 3:23 PM
Compilation and ASP.NET

Websites in Visual Studio follow two main project types - Web Application and Web Site.  The main difference between the two project types is in when the pages get compiled.  Pages in a web application are fully compiled into a single DLL when the project is built, and only the DLL is needed on the production web server to run the site (not ASPX, ASHX, ASCX, VB, or CS files).  With a web site however, the commonly used code such as App_Code master pages, web.config, and Global.asax are compiled into a group DLLs at project build time, but individual pages are only compiled on a per-directory basis when they are first requested.  All source code files are also copied to the production server, and whenever the source files change, ASP.NET will automatically recompile the DLLs to run those pages.  This means that with a Web Site, you can fully edit and modify the website directly on the production server if you want (and break it too, if you're not careful), but with a Web Application all the server needs is the DLL, and you can't edit the code part of the site from the production server without decompiling it or directly editing the .NET IL in the DLLs.

A solution in Visual Studio or Visual Web Developer can also have a mix of Web Application and Web Site projects, along with basic class libraries.  One subtle problem you may notice if you try to copy code from a Web Application to a Web Site is that a page in a Web Site project has a CodeFile attribute in the @Page header, but Web Application pages have CodeBehind attributes in the @Page header.  The two attributes basically do the same thing, but Web Application pages will ignore CodeFile attributes, and Web Site pages ignore CodeBehind attributes.   The compiler will not warn you if a page has the wrong kind of attribute for the project that it belongs to, and a page can even have both attributes if you want to be able to copy the code worry-free between the types of projects.  I am guessing that either no warning is provided by the compiler either to allow you to do this, or because Microsoft didn't think it would be a standard problem that we'd need to be warned about.   The net result is that if you're moving code from one type of project to the other, make sure it has the correct CodeFile or CodeBehind @Page attribute, or the page will not compile.

Another small point is that in a web application project, designer code files (such as MyPage.designer.cs) are created immediately when the ASPX file is created, and they are visible to you at all times in your project.  But in a web site project, the designer files are created just in time from the ASPX pages before they are needed, and they are not visible to you unless you look at the temporary compliation folder after the web page runs.  If you copy a page from a web application to a website, the designer file will be copied over but will not display as belonging to the page (it will show up next to the ASPX file, rather than under it), and it can be safely deleted.