Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Looking Behind ASP.NET’s Compile Model


asp_template

ASP.NET is popular among developers for a variety of reasons, including its compilation model, and rightly so. The first time an ASP.NET page is requested, the runtime parses the page’s markup code, produces a C# or Visual Basic .NET class, and finally compiles it on the fly. The newly created assembly is automatically loaded in the page’s AppDomain, and the contained class that represents the requested resource is instantiated. At the same time the dynamic assembly is created, an XML companion file is created in a temporary folder to preserve link information between the assembly name and the originally requested ASPX resource. In this way, whenever the ASPX file is updated, the linked assembly is invalidated. Upon the next request, the assembly would be recreated. Clean and effective.

However, many (should I say, all?) developers use Visual Studio .NET to create their applications. And Visual Studio .NET promotes the code-behind model for source code. What's that, you ask? Basically, a C# or Visual Basic .NET class file is associated with each ASPX markup file and contains any glue code needed to the page. At the very minimum, a code-behind class contains a handler for page events such as Init, Load, and PreRender. The key thing that is going on here is that each ASP.NET resource is split in two: a markup file (whose extension is .aspx) and a code-behind file (whose extension depends on the language of choice).

If the ASPX markup file gets modified, the next request triggers the compilation mechanism and generates a newer assembly. If the code-behind class is modified, instead, nothing of the kind happens. What's up?

In a Visual Studio .NET project, all class files are compiled into a single assembly, which is then deployed with the ASPX endpoint files. Unlike ASPX endpoints, no C# or Visual Basic .NET class file is ever deployed in the production environment. Hence, there's no way for the runtime to detect changes on class files and update the related assemblies. However, even if you decide to deploy class files on the server, nothing would happen anyway because the ASP.NET runtime is not configured to monitor C# or Visual Basic .NET classes for changes, unless these classes are explicitly bound to ASPX endpoints. As you may have guessed already, this just doesn't occur with code-behind classes.

Visual Studio .NET uses the CodeBehind attribute in the @Page directive to track page-class associations. Unfortunately, the ASP.NET runtime doesn't recognize this attribute. The ASP.NET runtime, instead, recognizes the Src attribute, which plays a similar role outside the realm of Visual Studio .NET. For any ASPX page that references a class file through the Src attribute, the ASP.NET runtime requires the class be available on the server and compiles it on the fly. In this case, any changes to the class file are promptly detected and lead to a new assembly.

Is there a way to combine the two models together? Sure.

You create an ASPX page in Visual Studio .NET and let Visual Studio .NET create a code-behind class for you. Next, you select the code-behind class in the Solution box and set the Compile property to None in the Properties box. At the same time, you add an Src attribute to the ASPX endpoint that links the code-behind class. You don't lose any of the Intellisense facilities that only Visual Studio .NET delivers, and you're still able to introduce changes to classes that are automatically detected without an intermediate compilation step.

As a final note, be aware that with ASP.NET 2.0, you'll enjoy a brand new compilation model that resolves these issues at the root.


Dino Esposito is Wintellect's ADO.NET and XML expert, and a trainer and consultant based in Rome, Italy. Dino is a contributing editor to Windows Developer Network and MSDN Magazine, and the author of several books for Microsoft Press including Building Web Solutions with ASP.NET and ADO.NET and Applied XML Programming for .NET. Contact Dino at [email protected].



Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.