I will make my first Linux web application in Microsoft’s “.net” platform. Since I do not run Windows I am going to using Mono (“.net for Linux”) and the MonoDevelop IDE. I am not so much a fan of ASP.net, so I’ve chosen Nancy instead which is a light-weight and simple replacement of ASP.net.
Nancy is a lightweight web framework for the .Net platform, inspired by Sinatra. Nancy aims at delivering a low ceremony approach to building light, fast web applications.
Xamarin is the company that allows for cross-platform multi-target (iOS, Android, Mac & Windows) development in C#. They maintain a recent build of the MonoDevelop application (currently 5.5). This tutorial may either be used for programming in C# or VB.net on Linux. C# does have support for code completion, something VB.net is missing in MonoDevelop.
To install the Xamarin version we have to add the Xamarin key and the repository by executing the following commands in any Debian based Linux (like Ubuntu/Mint):
sudo apt-key adv --keyserver pgp.mit.edu --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list sudo apt-get update
After that we can install everything using the following one-liner:
sudo apt-get install monodoc-base monodevelop mono-vbnc
Creating the project
After these installs (takes about 200 MB) you will have the “MonoDevelop” application (version 5.5) installed and you can start it from the “Development” menu. When you start it, it will ask you what you want to do. Choose to create an “Empty Project (VBNet)”. If you want to use C# then choose “Empty Project (C#)”.
If you get an error stating “Error while trying to load the project”, “Project does not support framework ‘.NETFramework,Version=v4.5′”, then you may want to open the project XML file (either “test.vbproj” or “test.csproj”) and replace the value that is defined as the “TargetFrameworkVersion”:
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
becomes:
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
You can see below how that looks in my MonoDevelop:
And then we can go look for Nancy.Hosting.Self via Project > Add Packages
When the “Add packages” window opens type “Nancy.Hosting.Self” in the search field:
Select the package and click “Add Package” to add it to your project. Great! Now it is time for some code.
Nancy’s Hello World example
First we make sure we start our host. In the Application.vb file.
Option Explicit On Option Strict On Imports System Public Class Application Public Shared Sub Main() Dim host As Nancy.Hosting.Self.NancyHost host = New Nancy.Hosting.Self.NancyHost(New Uri("http://localhost:8080")) host.Start() Console.ReadLine() End Sub End Class
And then we create a new Class “HelloModule” and add this code to it.
Option Explicit On Option Strict On Imports System Public Class HelloModule Inherits Nancy.NancyModule Public Sub New() Me.Get("/") = AddressOf Hello End Sub Public Function Hello(ByVal parameters as Object) As String Return "Hello world" End Function End Class
If we use C# then the Application.cs file looks like this:
using System; public class Application { static void Main(string[] args) { Nancy.Hosting.Self.NancyHost host; host = new Nancy.Hosting.Self.NancyHost(new Uri("http://localhost:8080")); host.Start(); Console.ReadLine(); } }
And we create a second class “HelloModule” with the following content.
using System; public class HelloModule: Nancy.NancyModule { public HelloModule () { Get["/"] = hello; } public String hello(Object parameters) { return "Hello world"; } }
Running your Nancy application
If you press run and you might get the following error:
Error CS0246: The type or namespace name `Uri' could not be found. Are you missing an assembly reference?
Click on “Project > Edit References”, then type “System” in the search field, select the reference and click “OK”. If you get the following error:
/usr/lib/mono/4.5/Microsoft.VisualBasic.targets: Error: Error executing task Vbc: Argument cannot be null. Parameter name: pathToTool
In case you get the above mentioned error, again, go to “Build\General” and under the section “Build Engine” uncheck the “Use MSBuild build engine …”
And now open a browser and go to: http://localhost:8080/
If the page does not load it maybe because the application already exited. This can be solved by clicking “Run on external console” under “Project > Options > Run > General”:
And now press run, everything should work and you should see some “Hello World” magic!
Finally..
You will learn that the MonoDevelop environment works flawlessly for C#. It also works quite good for VB.net, but it has some rough edges. The Nancy framework will feel very familiar if you have experience with frameworks like Sinatra, Silex or Flask. Mono is great for programmers that prefer Linux and in most application development it does not limit your possibilities. If you are going to run Nancy code in a production environment, then you may want to read about running Nancy behind a Nginx webserver.
NB: If you like this article you may also want to look into running ASP.NET vNext on Linux as well explained by Graeme Christie.
The post Web development in VB.net or C# on Linux with Nancy appeared first on LeaseWeb Labs.