Read our updated article on this topic here
ASP.NET is the latest Web server-side scripting language by Microsoft and the successor of ASP (Active Server Pages) 3. ASP.NET has traditionally been available for Microsoft's Windows platforms and its IIS (Internet Information Server) Web server. On Windows, enabling IIS to serve ASP.NET Web pages is as simple as installing the MS .NET runtime. In this article we see that setting up ASP .NET on Linux is equally simple.
Language Attributes As of now you can also use C# for writing ASP .NET pages on Linux. VB .NET is not supported. Hence, in your ASP .NET pages, you don't need to set the language attribute as <%@ Page Language=”C#” %> |
PROJECT MONO
Mono packages .NET library classes, a C# and VB .NET compiler, and the .NET runtime-CLR for Linux. Installing the Mono package (available as an RPM) installs the .NET runtime on Linux. We assume that you are running PCQLinux 8.0 (given out with the PCQuest March 2003 issue) machine. Though PCQLinux 8.0 bundles the Mono RPM, you'll find the latest version of Mono (0.24) on this month's Xtreme CD. It is recommended that you install/upgrade. For this, mount this month's CD and change to the directory system\cdrom\unltdoss\mono and install the RPMs found in this directory using the following commands:
rpm —Uvh libgc-6.1-1.i386.rpm
rpm —Uvh mono-0.24-2.i386.rpm --nodeps
The libgc RPM is required by Mono, hence you must install it prior to the Mono RPM. While installing libgc RPM if you get a message saying that the package already exists, just ignore and proceed with installing the Mono RPM. Henceforth, your PCQLinux machine is .NET ready.
Setting up Apache
Though your PCQLinux machine is .NET enabled, the Apache Web server still needs a piece of software to serve ASP .NET pages. First, make sure that you have Apache Web server installed. Issue the following command:
rpm —qa | grep httpd
If you don't get any output, this means Apache is not installed on the PCQLinux machine. In this case, mount the PCQLinux 8.0 CD 1 and install the RPM named httpd-2.0.40-11.i386.rpm found in the PCQuest/RPMS subdirectory on the CD. Next, mount this month's CD and change to the directory system\cdrom\unltdoss\aspdotnet and install the RPM mod_mono-0.3.7-1.i386.rpm as:
rpm —ivh mod_mono-0.3.7-1.i386.rpm
Restart Apache using the following command:
/etc/rc.d/init.d/httpd restart
All done. Your Apache Web server is ready to serve ASP .NET pages.
First Looks
A number of pre-written ASP .NET pages get installed (with mod_mono RPM) under the directory /var/www/html/mono. To see them working, key in the following URL in your PCQLinux Web browser (Mozilla or
Konqueror): http://127.0.0.1/mono/index.aspx
You will get a listing of all the ASP .NET pages as hyperlinks. Click on the hyperlinks to invoke the pages. You should be able to see the various ASP .NET Web, HTML and validator controls working on Linux.
ASP.NET AND MYSQL
___________________________________
To connect to a database, a .NET language requires what is called a data provider (kind of database drivers). A data provider is usually packaged in a .NET assembly. A .NET assembly file has a .DLL extension (even on Linux).
There are two data providers available for MySQL: ByteFX.Data.MySQLClient and Mono.Data.MySQL. Mono's installation installs the assemblies for both the data providers named ByteFX.Data.dll and Mono. Data.MySql.dll, respectively. However, according to Mono's website (www.go-mono.com), Mono.Data. MySQL has been deprecated.
Hence, in this article we use the ByteFX.Data.MySQLClient data provider to connect to
MySQL.
We now explain the steps and syntax to connect and query the database. We assume that you are familiar with the basics of ASP.NET and accessing MS SQL Server using ASP .NET. While going through the following code snippets, notice that the syntax, classes and methods/functions used for querying MySQL are very similar to those used for MS SQL Server. Just the word SQL in the class or method name is replaced with MySQL. For example, SQLConnection becomes MySQLConnection and SQLDataAdpater becomes MySQLDataAdpater. The steps are as follows:
- Link the ASP.NET page to the data provider's assembly using the following statement.
<%@ Assembly Name="ByteFX.Data" %>
- Alternatively, you can copy the file named ByteFX.Data.dll found in the directory /usr/lib to /var/www/html/mono/bin. In this case, you don't need the above statement.
Import the required namespaces as follows:
<%@ Import Namespace="ByteFX.Data.MySQLClient" %>
- Specify the connection parameters as:
MySQLConnection conn = new MySQLConnection("Server=localhost;Database=pcquest;User
ID=root;Password=foo;");
In the above statement, replace localhost with the name or IP address of the machine running the MySQL database. Replace pcquest with the name of the database you wish to connect to. Substitute root and foo with the username and password required to connect to the database.
Issuing Insert, Update and Delete Queries
Now we see how to issue SQL queries, which don't return data from the database like Insert, Update and Delete queries.
- Construct the SQL query using the following statement:
Ready for Primetime?
By deploying ASP .NET on Linux, you can do away with a dedicated Windows server on your Intranet or Internet, reducing costs considerably. Surprisingly, a couple of months back, Netcraft had reported that 1% of ASP.NET sites are run on Linux using Mono (refer to |
MySQLCommand cmd = new MySQLCommand("
Substitute
- Open the connection to the database as:
cmd.Connection.Open( );
Execute the query as:
cmd.ExecuteNonQuery( );
- Finally close the connection as:
cmd.Connection.Close( );
Issuing Select Queries
In this section, we see how to issue SQL Select queries against the database to retrieve and display the data on the Web page. With ASP .NET the easiest way to display the tabular data from the database is to use the DataGrid component. The following code uses the same.
ASP.NET on Linux without Apache
We have said that installing mod_mono RPM will enable Apache to serve ASP.NET pages. What we haven't mentioned is that mod_mono RPM installs a Web server called XSP that does the work of processing ASP .NET pages. Whenever Apache is asked for any page residing in the directory /var/www/html/mono via the URL http://127.0.0.1/mono, Apache hands over the processing to the XSP Web server and returns the result. This is very similar to the Apache-Tomcat integration. You may like to have a look at the file named mono.conf in the directory /etc/httpd/conf.d. This two-line configuration file sets up Apache to serve ASP .NET pages using XSP. You can also run XSP standalone. For this, first install the Mono RPM as explained in the section Thanks to Project Mono of this article. Copy the file named xsp-0.4.tar.gz found on this month's PCQEssential CD in the directory system\cdrom\unltdoss\aspdotnet to /opt directory of PCQLinux. Extract the archive as |
After connection to the database using MySQLConnection (as explained above), construct a data adapter, to bind to the data grid, as follows:
MySQLDataAdapter adapter = new MySQLDataAdapter("
Substitute
The remaining code to use the data grid to display the data retrieved by the select query remains the same, as in, same as for
MS SQL Server.
DataSet dataset = new DataSet( );
adapter.Fill(dataset,"
grid.DataSource=dataset.Tables<"
grid.DataBind();
The above code fills the retrieved data from a table named
Full-fledged Database
On this month's CD you can find ASP.NET pages, which interact with a MySQL database named pcquest. This database consists of a table named designations, which consists of two columns, name and role. The name column stores the full name of an employee and the role column stores the role/designation of the employee.
Copy the files named insert.aspx and display.aspx found in the directory system\cdrom\unltdoss\code on this month's PCQEssential CD to /var/www/html/mono directory on the Linux machine. insert.aspx inserts data into the pcquest database while display.aspx displays the inserted data. Note that if you are writing your own ASP .NET page, you must place the files in this directory.
Set up MySQL
Check whether MySQL is already installed on your PCQLinux machine by issuing the following command:
rpm —qa | grep mysql
This should show you the following two RPMs (amongst others).
mysql-server-3.23.54a-4
mysql-3.23.54a-4
If this is not the case, install the RPMs named mysql-3.23.54a-4.i386.rpm and mysql-server-3.23.54a-4.i386.rpm found on PCQLinux 8.0 CD2, under the PCQuest/RPMS directory. Use the following commands to install the
RPMs:
rpm —ivh mysql-3.23.54a-4.i386.rpm —nodeps
rpm —ivh mysql-server-3.23.54a-4.i386.rpm
Now start the MySQL database server by issuing the following commands:
/etc/rc.d/init.d/mysqld start
Now create a database named pcquest by issuing the following command:
mysqladmin create pcquest
To set up the table for the pcquest database, we have given out a script file named createdb.script on this month's PCQEssential CD in the directory system\cdrom\unltdoss\code. Mount the CD, change to this directory and issue the following command:
mysql —u root < createdb.script
MySQL does not require a password for “root” by default. If you have set up a password, you must open the files insert.aspx and display.aspx in a text editor and modify the connection string passed to method/function MySQLConnection( ) to include your password.
Now, first access the page insert.aspx as http://127.0.0.1/ mono/insert.aspx and enter the name and designation of an employee and click on the button labeled “Insert into database”. When you load up display.aspx (http://127.0.0.1/mono/display.aspx) you should be able to see the data you had entered.
Shekhar Govindarajan