C# – With Web projects Asp.Net only detects changes in aspx, but not cs files

asp.netasp.net-2.0assembliesc

Q1 – Asp.Net is able to detect when you change the original files and automatically recompilles the application when next request arrives

A) But it appears that while with Web sites Asp.Net is able to detect changes, regardless of whether they happen in code-behind file (.cs) or in aspx file, with Web projects Asp.Net only detects changes that happen inside aspx file, but it doesn’t detect changes inside .cs files! Why is that?

B) Asp.Net also detects if any new aspx or cs files were added to the website. But, will it upon the next request compile just the added files, or will it recompile all of app’s files?

C) If an application on a web server contains several aspx files, some created by web project ( and thus their code behind files are inside single assembly ) while other aspx files were created via a website project, how is Asp.Net able to figure out whether particular aspx file has code behind contained within an assembly located in Bin directory?

Q2
“Codebehind attribute is used only by the Visual Studio Web Forms Designer. The attribute is not used at run time.”

  • I assume CodeBehind attribute is not used at runtime due to the fact that all .cs files are compiled into single assembly, and thus VS has no problem locating a class as long as aspx tells the name of the class via Inherits attribute?!

  • On the other hand, Asp.Net code behind model does need src attribute, because here cs files get compiled into separate assemblies and thus asp.net needs to know the name of the file specified by CodeFile attribute, so it can somehow figure out the name of the assembly into which this file compiled?

thanx

Best Answer

ASP.NET only looks for a code behind file if it can't find the type it needs to complete the compilation of a ASPX in the existing assemblies. A dependancy on the code behind cs file is only created if it was needed when building the temporary assembly for the ASPX file.

Therefore where in a web project all the codebehind cs files are compiled into a single assembly the compilation of a temporary assembly for the ASPX does not require the code behind cs, since the type(s) needed are already present in the pre-built assembly. Hence no dependancy is created between the temporary assembly and the cs file so changing the cs file does not invalidate the existing temporary assembly. Note in deployment the cs files won't even be present.

In a web site there is no pre-built assembly containing all the types in code-behind, hence the code-behind file is required to complete the temporary assembly for an ASPX, in turn a dependancy on the cs file is added to that temporary assembly. Hence any change to the cs file invalidates the temporary assembly.