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
posted on Thursday, March 23, 2006 5:11 PM
Feedback
# re: Compilation Error executing ASP.NET Page.
3/26/2006 4:27 PM |
You can actually view the framework generated code by setting a breakpoint into your constructor and stepping through. Anyway, why would you need a private constructor for a Page derrived class?
# re: Compilation Error executing ASP.NET Page.
3/27/2006 7:07 AM |
You don't need a constructor in a Page. I just want to demonstrate in a funny way the ASP.NET inheritance, and what happens with the *.aspx code.
# re: Compilation Error executing ASP.NET Page.
3/27/2006 8:00 PM |
Sometimes you do need a constructor in a Page derrived class, sometimes you're using one without "realizing" that. Even if it seems that you don't use a Page constructor, I'm sure you know that an empty one is created on your behalf and if you declare some field and initialize it, that code is going into that default constructor. That default constructor will be called every time a Page is created (by the framework, by you using the new keyword, etc.) with just one exception: when you clone a Page (derrived) object.
Yes I understood your point here, and it's a good start. If you'll have the time to deep into it a little more in a future post, I'd be glad to keep commenting.
# re: Compilation Error executing ASP.NET Page.
4/3/2006 7:30 AM |
Hello Adi. I'm not sure about what do you mean with: when you clone a page...
The problem with the constructors in ASP.NET pages is that, at this point, you can't see common objects you usually use like Session, Cache, Application, ViewState... You can't also have access to the control tree and so many things, so this is why I'd said that using constructors in ASP.NET Pages is useless. Of course in OOP every class need a constructor, explicitly declared or a default one, but in this case, I think is better to let the framework works with the default one.
# re: Compilation Error executing ASP.NET Page.
4/4/2006 12:33 AM |
I ment cloning a Page derrived object with the MemberwiseClone() internal CLR method. I think that's the only way to "create" a Page without executing the constructor.
I agree with you that 99% of the time there's no need to initialize stuff in a Page constructor explicitly, I just said that "Sometimes you do need a constructor in a Page derrived class...". I could show you an example of such a situation, but I'm affraid there's too much code involved and a little dificult to understand in just 5 minutes. To make yourself an idea, it's about initializing the Form in a custom base Page before any other control initialization, overriding the OnInit and calling the base implementation after your's to be able to modify the control tree after that, and register a custom rendering method through a delegate. Like I said, it's a little dificult even to explain and I'm not sure you'll get the idea. If I'll have the time, and you're interested I'll try to explain it better, but I'm sure you agree that there "could be" a useful use of a Page constructor.
# re: Compilation Error executing ASP.NET Page.
11/18/2007 8:38 PM |
thxx
# re: Compilation Error executing ASP.NET Page.
7/16/2008 6:01 PM |
thank you..