Check Out The ASP.NET 2.0 Whidbey Features!
When ASP.NET 1.0 was released, it revolutionized Web application development by providing a rich set of features that were aimed at increasing developers' productivity. Now with ASP.NET 2.0 (code-named Whidbey), Microsoft increased the bar to a much higher level by providing excellent features out-of-the-box that are targeted towards reducing the code required for building Web applications.
These new enhancements arm the developers with a powerful platform that can make a significant impact in the way Web applications are developed and maintained. Apart from increasing the productivity of the developers, ASP.NET 2.0 also provides a number of excellent features, such as easy administration and management of the platform, an extensible platform that can be easily customized to meet the requirements of any enterprise, increased performance, and so on.
In this article, I will present you with the features of this new improved platform and to help you understand how you can use it to design, develop, and deploy enterprise-class Web applications. We will start off our discussion by taking a look at the changes to the ASP.NET Page architecture.
Changes to the Page Architecture
One of the significant improvements in ASP.NET 2.0 is the support for Single-Page architecture. This means that any Web page that you create can contain the server-side logic in the same page itself, instead of storing it in a separate .aspx.vb or .aspx.cs file, as is the case with current .NET version. The server-side code will be contained in a script block with a runat=server attribute. However, the object model and event model are still the same as in the current code-behind model.
Another important feature is the ability to request a Web form (.aspx file) from a browser without having to compile the code even once. When the page is first requested, ASP.NET compiles the page on the fly, dynamically generating the assembly. This makes it possible for you to resort to the "Just Hit Save" programming model (as similar to ASP) wherein you just develop the page and test the page without having to compile it.
As mentioned before, by default, ASP.NET Web pages and code files are compiled dynamically when a first request is made to the page. After the initial compilation, the compiled page is cached; the cache is used to satisfy the subsequent requests for the same page. Even though this approach is flexible, you should note that that when the page is requested for the first time, it requires a bit of extra time to compile the code. You can avoid this overhead by leveraging a new feature known as Precompilation; by using this feature, you can compile an ASP.NET Web site before making the Web site available to the users.
By using Precompilation, you also can catch all the compilation errors before deploying the application onto the production servers. ASP.NET 2.0 provides the following two options for precompiling a site.
- In-place Precompilation—When you perform in-place precompilation, all ASP.NET files are compiled and stored in the special folder named codegen. The precompilation process follows the same logic that ASP.NET uses for dynamic compilation, also taking into consideration the dependencies between files. During precompilation, the compiler creates assemblies for all executable output and places them in the codegen folder. After the compiled output is created, ASP.NET fulfills requests for pages using the assemblies contained in this folder. One of the important advantages of precompilation is the ability to check the Web site for compilation errors. To precompile a Web application named NorthwindWeb (that is present in the default Web site), you need to enter the following URL in the browser.
http://localhost/NorthwindWeb/Precompile.axdThis command allows you to precompile the Web site and if there are any compilation errors, they will be displayed in the browser.
- Precompiling a site for deployment—By using this option, you can create a special output that can be deployed to production servers. Once the output is created, you can deploy the output using various mechanisms such as XCOPY or FTP, or Windows installers onto the production servers. To precompile a Web site for deployment, you need to use the aspnet_compiler utility.
Use of Master Pages
ASP.NET 2.0 introduces a new concept known as Master Pages, in which a common base master file is created to provide a consistent layout for all the pages in your application. Once you isolate the look and feel and standard behavior for all the pages in your application, you can move them to a master page. In the master page, you also add placeholders (known as ContentPlaceHolders) for the content (or child pages) to add their custom content. When users request the content pages, the output of the content pages is merged with the output of the master page, resulting in an output that combines the layout of the master page with the output of the content page.
Master pages are saved with the file extension .master. Apart from containing all the contents that are required for defining the standard look and feel of the application, the master pages also contain all the top-level HTML elements for a page, such as <html>, <head>, and <form>. The master pages also contain one or more content placeholders that are used to define regions that will be rendered through the content pages. Let us take a look at an example.
For the purposes of this example, let us create a new page named MasterPageExample.master and add the following lines of code to it.
<%@ master language="C#" %> <html> <head runat="server"> <title>Master Page</title> </head> <body> <form runat="server"> This content is from the Master Page from which the all the pages will be inherited <br /> <b> <asp:contentplaceholder id="MiddleContent" runat="server"> </asp:contentplaceholder> </b> </form> </body> </html>
In the above lines of code, we start by defining a new page directive named master. As the name suggests, this declarative is used to identify that the current page is a master page and prevents the users from requesting the page from a browser. Then, inside the code, we define an element named asp:contentplaceholder, which will be used by all the content pages to render appropriate content that is specific to their pages. Now that we have had a look at the master page, let us look at the content page. To create a content page, let us create a new ASP.NET page named ContentPageExample.aspx and add the following lines of code to it.
<%@ page language="c#" master="~/MasterPageExample.master" %> <script runat="server"> </script> <asp:content id="Content1" contentplaceholderid="MiddleContent" runat="server"> This content is from the child page </asp:content>
The code required for the content page is very simple and straightforward. We start by defining the Page directive. As part of the Page directive, we also specify a new attribute named master that is used to identify the name of master page that we want to use. Since we have already created a master page named MasterPageExample.master, we will specify that as the master file. Next, we have an element named content that is used to associate the contentplaceholder element in the master page with the content page. This is done through the use of the contentplaceholderid attribute. That's all that is there to creating a master page and using the master page from a content page. Now, if you request the content page from a browser, you will get the following output.
Advantages of Master Pages
Master pages provide a clean approach for encapsulating the common functionality in a centralized location, allowing other pages to inherit from a single master page, thereby reducing the maintenance of the application.
- Master pages make it easy to create one set of controls and code and apply the results to a set of pages. For example, you can use controls on the master page to create a menu that applies to all pages.
- They provide a fine-grained control over the layout of the content pages by allowing you to control how the placeholder controls are rendered.
- Master pages also expose an object model that allows you to customize the master page from individual content pages.