How .aspx and aspx.cs file is being compiled? will it be two assemblies or both these files combined one and will be created as one assembly? Does any hierarchy/inheritance it follows? .aspx is being created as an corresponding C# code file (only then we could create an assembly right? correct me if i am wrong!) as one in windows form designer.cs file?
Share
It depends on the project type. With ASP .Net you can create two kind of projects: Web Application Project and Web Site Project..
How .aspx and aspx.cs file is being compiled?
With
Web Applicationproject you compile the code behind of theaspxfiles, user controls and other code found in this project into one single dll and deploy it on server.With
Web Site Projectyou simply copy the source code on the server andASP .Netwill handle the compilation for you. The Web Site Project holds the source code of custom classes in App_Code folder (you’ll have to read more about these on that link)Will it be two assemblies or both these files combined one and will be created as one assembly?
In none of these cases, the code found in the
aspx, ascxfiles is not compiled by you (using Visual Studio, etc).ASP .Netparses these files and creates a dll stored in its temp folder. The “aspx, ascx” dlls (it can be more than a single one though) are different files than the one created by you with Visual Studio (and I believe is different than the one created from App_Code folder, since that code can’t access code found in the pages).Does any hierarchy/inheritance it follows?
Yes. When a page is parsed and compiled, that generated class will inherit the one named at “Inherits” attribute, found at @Page directive.
ASP .Net parses Default.aspx file and generates a class which inherits
WebApplication1._DefaultGiving the fact that the generated class from markup inherits a class written by us (usually the one in the code behind, the
aspx.csfile), we’re free to use its members or methods.The following method is a method of the _Default class:
Then in markup we can call:
We can even write in markup something like:
Where SomeValue it’s at least a protected property of the _Default class.
We’re free to declare members and write server code in the markup also:
Here I declare a
someCounterfield and use it to write 10 paragraphs. Of course this is not the recommended way to do such things. SincesomeCounteris a member of the generated class, this is not accessible in code behind.There is another huge (and more real) advantage of this architecture. Suppose some pages in the web site are kind static (about.aspx, privacy.aspx, etc), they use same Master page. The code behind in this pages doesn’t change. This means we can create other pages and deploy them without doing another compilation of the code behind (this aspect applies to Web Application Projects). Also, before going live, we might allow only one person to see these pages. So to achieve this we create a PreviewPage class
And change the
Inheritsvalue:aspx is being created as an corresponding C# code file
The
Languageattribute in the@Pagedirective dictates what language is used to compile theaspx/ascxfile. So you can actually use VB.Net in the aspx file and to compile the website with C# compiler.This being another compilation, different than the one Visual Studio does, it is made with different options. This is why in web.config there are options to set the
compilationModetoDebug/Releaseand also to instruct the compiler to use the other available options.