posts - 23,  comments - 82,  trackbacks - 0
  Thursday, March 23, 2006

I know the title is strange. Compilation error trying to execute a page? Well, continue reading and you will understand.

You know all languages in .NET are OOP languages, so pages in ASP.NET are also objects. When you compile an ASP.NET project, you just compile the code-behind code but, what happens with the *.aspx files? The code in this files is parsed to several *.cs files (one per page) and compiled on demand.

Trying to give a funny speech to my team about ASP.NET best practices, defensive programming, standards..., I began to play with a page and place a default constructor on the code behind. Nothing happens. Ok, is a class and it could have a constructor. You really can't do something useful here, but you can code it.

Try again with a private constructor. Compile the project and execute. Here you have the "Compilation Error executing ASP.NET Page":

Compiler Error Message: CS0122: 'DemoCharla.AspxWithConstructor.AspxWithConstructor()' is inaccessible due to its protection level.

As you know, in the @Page directive in *.aspx file you specify Inherits attribute. This really means that the *.aspx files inherits code-behind classes, but obviously, after parsing them to corresponding *.cs files and compiling.

Just one useful thing is that you can see the source code of the generated *.cs file in the error page, clicking in the "Show Complete Compilation Source" link This is the same as you can see in the "Temporary ASP.NET Files" folder behind \Framework\< version >\ folder. If you go there and look inside your folder project, you will find a folder with a name like 5e170680 (this name is always different, but have the same format) and inside this folder you will find another folder with similar name. Here is where all the temporary files are generated. You can see *.cs files, *.cmdfiles which contains necessary parameters for compilation, the *.out and *.err as a result of this compilation and more.

You can try and enjoy for a while seeing all this files, and executing a page and seeing csc.exe in the process monitor compiling pages for the first time. Isn't really useful but is what really happens "behind the scenes".

There's a good article in Code Project from Natty Gur: http://www.codeproject.com/aspnet/ASPXFILES.asp