by July 9, 2003 0 comments




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
www.entmag.com/news/article.asp?EditorialsID=
5638)

But while working with Mono and ASP .NET, we found that a popular Web component, AdRotator, does not work as expected. AdRotator, which is used to display random advisements, displays only one ad (the first ad specified in the XML file). We have filed a bug report on it at
http://bugzilla.ximian.com  (Bug ID: 44271). As per the status report at
www.go-mono.com , porting efforts are going on. Hence, while working with Mono ASP .NET you may discover more bugs or unexpected results. 

On the flip side, in this article itself, we have demonstrated a full-fledged database application that works smoothly. Depending on your needs, you may never need use one of the buggy components. But, if you feel that the real power of ASP.NET lies in its components, then test, test and again test the components thoroughly before using them. Considering the cost benefits, it is worth giving Mono ASP.NET a try. You can at least try porting your Intranet applications, if not the ones deployed on the Internet.

MySQLCommand cmd = new MySQLCommand(““,conn);
Substitute with an Insert, Update or Delete query. Note that the parameter conn is the MySQLConnection object constructed in the previous statement.

  • 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

tar —zxvf xsp-0.4.tar.gz

This will create a directory named xsp-0.4 under /opt. Change to this directory and issue the following commands
make 
make install

Change to the directory /opt/xsp-0.4/server/test and issue the following command

mono server.exe &

This will start the XSP Web server at port 8080. You can now browse the ASP .NET pages using http://127.0.0.1:8080/index.aspx.
All the ASP .NET files reside in the directory /opt/xsp-0.4/server/test. To see your ASP .NET pages/files working, you must place your files in this directory and access them as http://127.0.0.1:8080/.aspx.

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(““,conn);
Substitute with a valid SQL Select query.

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[““].DefaultView;
grid.DataBind();

The above code fills the retrieved data from a table named and shows it using a DataGrid component having the name/ID as grid. 

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

No Comments so far

Jump into a conversation

No Comments Yet!

You can be the one to start a conversation.

<