alt
Advertisement
Sponsored links
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow ASP.NET 2.0 arrow ASP.NET 2.0 Code Directory
Site Search


ASP.NET 2.0 Code Directory
Article Index
ASP.NET 2.0 Code Directory
Page 2

ASP.NET 2.0 Tutorial : Code Directory

In this tutorial you will having a deeper look at the Code Directory, the Code Beside Model and the evolution of the Code Behind model, learn about Partial Classes, Sharing Source components, Creating a Component, you will learn how to create a Application_Code folder, to create a component in the Application_Code folder and use a component.

 

Having a deeper look at the Code Directory

As mentioned in the earlier section of this tutorial the Code directory is one of the significant folders of the ASP.NET application. It contains the reusable components that are automatically compiled and linked to the page code by the runtime engine. Visual Studio 2005 gives a lot of importance to the monitoring of this directory. The components are compiled by default into a single assembly and referenced in the project. The assembly is then made available to all the pages in the site.

The Code Beside Model and the evolution of the Code Behind model

The Code Behind class model has been evolved to get round the problems of using inline code in Visual Studio 2003. Using inline code automatically deprived the user of the benefits of the IntelliSense. The Visual Studio 2003 model is based on the idea that each Web form page is bound to a separate class file and this file is the basis of the dynamically generated page class that the ASP.NET runtime creates for each .aspx resource. All server code is channelled to the code behind class and everything compiles down to an assembly in which all the constituent classes are packed together. This becomes a handicap. An explicit step by step compilation is demanded. Every page must reference the code behind class through a src, else it will not be compiled. The AppDomain has to be restarted with every change and the timestamp of the directory is modified. If the project is large, the whole process is expensive.

ASP.NET 2.0 offers the compile on demand feature to the users. All resources are compiled only on demand with an exception of a few file types which are dynamically compiled. ASP.NET pages, Web Services, user controls, HTTP handlers and global.asax are dynamically compiled on demand. Any changes to the compiled copy is automatically detected and the copy is invalidated and a fresh compilation takes into account the changes. This reduces the process overhead for developers and helps in rapid application development.

The compile on demand feature has been extended to a number of file types in ASP.NET 2.0. Class files, resource files, Web service discovery files, and typed DataSet schema files have all acquired this capability. All changes to files are promptly and automatically detected and no development tool is required to force the compile step. It becomes possible to refine the whole code behind mechanism. Since ASP.NET is backward compatible it works with the code beside schema of older applications with ease.

The code beside model provides for a modular approach by separating support code and layout. It comes with a page/class association and uses a different set of keywords and behaviours. By default the code is stored inline. To take advantage of the code separation the user has to select a template by selecting Add New Item form the Website menu. The pages can be edited with page separation and the code is stored in a class file specified through the CompileWeb attribute. ASP.NET then compiles the page when requested.

Partial Classes

The page separation feature enables developers take advantage of partial classes. The CompileWeb attribute in the @Page directive is used to find the file containing the code. The .aspx page and the code page are then dynamically merged into a single class that inherits the base page class. This is then compiled into an assembly and executed. By default the code in the code beside file is located in the ASP namespace and contains only the class definition which is partial. It however contains the event handlers and other custom code that the user writes. The ASP.NET 2.0 runtime parses the .aspx layout file and combines the information of the partial class in the code beside file. The class then inherits from the Page and is compiled and served on request. The code beside version of our ‘Hello world’ application would look like this:

using System;
namespace ASP
..................{
...................public partial class Helloworld
..........................{
...........................void Send_Greeting(object sender, EventArgs e)
..................................{
....................................MsgSent.Text=Greet.Text;
...................................}
............................}
...................}

Sharing Source components

The compile on demand feature has been extended to the classes bound to the Web page in the code beside model. The helper components or other class files of the application can now be stored in the Code subdirectory. Visual Studio 2005 monitors the directory and compiles all class files that are added there. This enables the assembly to be referenced automatically after compilation. This assembly is named code.dll and has application scope. It is created in the Temporary ASP.NET files folder outside the Web application space.

To illustrate this concept let us create a simple project. To recall the steps for creating a new web site:

1. Open Visual Web Developer.

2. On the File menu, click NewWeb Site.

3. The New Web Site dialog box appears.

4. Under Visual Studio installed templates, click ASP.NET Web Site.

5. In the Location box, enter the name of the folder where you want to keep the pages of your Web site.

6. For example, type the folder name C:\WebSites.

7. In the Language list, select C#.

8. Click OK.

Visual Web Developer creates the folder and a new page named Default.aspx.

Creating a Component

Reusable components can be created by keeping them in a sub directory. Let us call this directory App_code. This directory is monitored by ASP.NET 2.0 and all components added to this directory are compiled automatically into a single assembly.

To create a Application_Code folder

1. In Solution Explorer, right-click the name of the Web site, and select Add

2. Select the folder Application_Code.

3. A component can now be added to the site.



 
< Prev   Next >
Exforsys Offers
© 2008 Exforsys.com
Joomla! is Free Software released under the GNU/GPL License.
Page copy protected against web site content infringement by Copyscape