PDA

View Full Version : Mono HOWTO: How to Include Classes in an ASP.NET Page?


Amit Yaron
January 30th, 2008, 09:59 AM
How to Include Classes in an ASP.NET Page?

The ASP.NET Quick Start Tutorial (http://quickstarts.asp.net/QuickStartv20/aspnet/Default.aspx) contains instructions and sample code that can help you understand how to work with ASP.NET. The Quick Start Tutorial contains a chapter named "Building a Web Application" (http://quickstarts.asp.net/QuickStartv20/aspnet/doc/pages/code.aspx), which contains a section named "Sharing Code Between Pages".
This article will teach you how to do it in Mono in your Linux system.

Where to Place Source Files?
In ASP.NET 2.0 you can add source files in a directory named 'App_Code'. This directory should be created in your application directory defined in "httpd.conf". (e.g. /var/www/<app name>/App_Code
If App_Code contains sub-directories, you should also add them to the file "web.config" in your application directory, the following way:

<configuration>
<system.web>
<compilation>
<codeSubDirectories>
<add directoryName="Subdirectory"/>
</codeSubDirectories>
</compilation>
</system.web>
</configuration>

Precompiled Assemblies
Precompiled assemblies are the '.dll' files generated by the relevant compiler. Assemlies should be placed in a directory named "bin" (The Bin directory in the Quick Start Tutorial). You may need to import a name space to use a class. In this case you should add the following directive at the top of the page:
<%@ Import Namespace="myNamespace" %>

Generating an assembly can be done using the "gmcs" compiler, which will be explained later.

The Global Assembly Cache

The Global Assembly Cache (GAC) is a versioned repository used for storing assemblies in ASP.NET 2.0. Assemblies stored in the GAC are available to all applications on the machine. To add an assembly from the GAC to your application, add them to the file "web.config" in your application directory:
"
<configuration>
<compilation>
<assemblies>
<add assembly="System.Data, Version=1.0.2411.0,
Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
</assemblies>
</compilation>
</configuration>
"
[ASP.NET Quick Start Tutorial]
If you want to use a name space, you should add the following Import directive in your "aspx" page:
<%@ Import Namespace="myNamespace" %>

To find the exact value of the assembly attribute, use the "gacutil" command:
gacutil -l <assembly-name>

The assembly -- or a link to it -- should be located in the "bin" directory.
Assemblies added to the GAC should be digitally signed. To create a digital signature ("snk" file) use the command "sn" the following way:
sn -k <snk file-name>

Creating a digitally signed assembly using the "gmcs" command will be explained later.
To add an assembly to the GAC use the "gacutil" command:
gacutil -i <assembly-name>

To learn more about "gacutil" type "man gacutil".

Creating an Assembly

One of the commands that builds an assembly("dll" file) is "gmcs".
A simple usage example is:
gmcs -reference:<reference assembly> -keyfile:<"snk" file> -out:<output assembly> <source files>

where:
reference assemblies - a "dll" file containing name spaces and classes used by the program.
source files - c# files with the suffix "cs".

To learn more about "gmcs" type "gmcs -help".