Wallpapers Collections

Wallpapers Collections

Wednesday, February 29, 2012

ASP.NET on Linux

We write a simple ASP.NET application on Linux using an IDE called MonoDevelop. The application will uses the ASP.NET Model View Controller framework and LINQ.



The single, popular solution to run ASP.NET applications, on Linux, is Mono (www.mono-project.com/). The reason being, Mono has come a long way to achieve near compatibility and cutting edge features of Microsoft .NET. Mono can be used to run .NET applications on Linux and Mac, besides Windows.
For developers, the exciting part about Mono is that it has started supporting the latest features in .NET like C# 4.0, ASP.NET MVC, LINQ and Moonlight. What's more, using Mono you can start writing .NET apps for Apple iPad, iPhone and iPod, today.
In this article, we write a simple ASP.NET application which uses ASP.NET MVC (Model View Controller) framework and LINQ. We will write this application, on Linux, using an IDE called MonoDevelop (monodevelop.com).
To keep the app simple, we will use a SQLite database which will be queried via LINQ (Language Integrated Query). And we w ill deploy the app on an Apache server, running on Linux.
Setup the prerequisites
The application in this article is written and deployed on Fedora 13. We used version 2.6 of Mono and version 2.4 of MonoDevelop. Installing Apache and Mono packages on Fedora 13 is as simple as issuing the following commands as root:
yum install httpd mod_mono
Fedora 13 ships with MonoDevelop 2.2. To install MonoDevelop 2.4, we compiled the MonoDevelop source RPMs found at http://elsupergomez.fedorapeople.org/SRPMS/. You can download the compiled RPMS from forums.pcquest.com and install them as 'rpm -Uvh <filename>'.
An ASP .Net MVC app
An ASP .Net MVC application breaks through the traditional Web Forms application, allowing full control and replaceable ways to design the UI and access a database. MonoDevelop makes it easier to write a MVC app. It generates the required skeleton and files - just like Visual Studio.
Fire MonoDevelop (Applications>
Programming>MonoDevelop in GNOME). Click on File>New>Solution. Under C#>ASP .Net, ­select ASP .Net MVC Project. Type in the name of the project and click Ok. This creates the skeleton code for an ASP .Net MVC application - see the Solution pane on the left.
You can see the MVC app working, already, by clicking on Run>Run With>Mono Soft Debugger for ASP .Net. You will be greeted by a Web page saying "Welcome to ASP.NET MVC on Mono!"
Add a database via LINQ
Next, lets create and add a simple SQLite database to the project. We will access this database using LINQ. In simple words, Language Integrated Query (LINQ) is a language to query the database as objects, rather than using SQL.
On MonoDevelop, click on Tools>Database>Create Database>SQLite Database. Enter a Name, say PCQuestDB and for Database type in pcquest.sqlite. The database will get created in your home directory (/home/<your-user-name>).
Next, select Tools>Database>Query Database. From the dropdown, in the bottom pane, select the database (PCQuestDB in our case) and type in the following queries:
create table article (id integer primary key, title varchar(100));
insert into article (title) values ('Title 1');
insert into article (title) values ('Title 2');
insert into article (title) values ('Title 3');
Click on the Execute button. Next to generate the corresponding LINQ classes, click on Tools>Database>Generate Linq Class. For Connection, select PCQuestDB and then click on Ok. This will add a new file called Output.cs to the Solution pane, on the left. Open this file and find the following lines:
using DbLinq.Linq;
using DbLinq.Linq.Mapping;
Comment out or delete these lines and add a line as:
using System.Data.Linq;
Next, open the file named Web.Config and add the following lines after the tag </configSections>
<add name="PCQDB" connectionString=
"DbLinqProvider=Sqlite;Data Source=/opt/pcquest.sqlite" />
Next, copy pcquest.sqlite file, found in your home directory to /opt directory.
Next in the Solution pane, right click on References and select Edit References. Click on the Packages tab and tick the checkbox against Mono.Data.Sqlite.
Note, in case you see System.Web.Mvc, in red, under References. Right click on System.Web.Mvc and select Delete. Next, right click on References and select Edit References. Click on the .Net Assembly Tab. Browse to the directory /usr/lib/mono/gac/System.Web.Mvc/1.0.0.0__31bf3856ad364e35. Select System.Web.Mvc.dll. Click on Add and then on OK.
The Controllers and Views code
On the Solutions pane, expand Controllers and click to edit the file HomeController.cs. Modify the code as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Web.Configuration;
namespace Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index ()
{
PCQuest.DbLinq.Main pcqDB = new PCQuest.DbLinq.Main(new Mono.Data.Sqlite.SqliteConnection(WebConfigurationManager
.ConnectionStrings["PCQDB"].ToString()));
var query = from q in pcqDB.Article
select q;
return View(query);
}
}
}
In the above code, note the use of LINQ to query the SQLite database.
Next, expand the Views>Home. Modify the code of Index.aspx as follows:
<%@ Page Inherits="System.Web.Mvc.ViewPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<div>
<ul>
<% foreach (PCQuest.DbLinq.Article article in (Ienumerable)ViewData.Model) { %>
<li> <%= article.Title %></li>
<% } %>
</ul>
</div>
</body>
Deploy on Apache web server
First, a small configuration change is required for Apache, to serve ASP .Net MVC apps. For this, login as root and open the file named mod_mono.conf found in the directory /etc/httpd/conf.d. Append the following line to it:
ForceType application/x-asp-net
Save the file and reload the configuration change by issuing "service httpd reload"
Next, create a directory named mvc under /var/www/html. For quick and non production deployment, give all permissions to this directory by issuing the command: chmod 777 /var/www/html/mvc.
On MonoDevelop, click on Project>Deploy to Web. Click on Edit Targets button. Click on Add and type in any Name. For Target, select Local Filesystem. For Target Directory, browse or enter the path: /var/www/html/mvc. Click on Ok. Back on the 'Deploying Web Project' window, select the Location and click on Deploy. Make sure that  the SQLite file copied in /opt (see above) is accessible to the Apache Web Server.
Browse to the URL, http://localhost/mvc and you should be able to see a simple list of all the articles' titles in the SQLite database.
The take home is: with Mono, Linux can be used as a platform to develop as well as deploy cutting edge ASP.Net applications.

No comments:

Post a Comment