.Net Web dev Mac – Nuget, Monodevelop and Nancy

Basic Support

In the Beginning

Managing binaries, dependencies and packages is such an important point that it is unbelievable  that it takes 10 years to get something like Nuget on .Net from Microsoft…

If you are running Mono for .Net web development on your Mac, it is not better regarding this point…

It was painful with MonoDevelop was the lack of support for Nuget.

We can do things by hand since v2 of Nuget (and before 1.7 btw) because you can use nuget.exe from mono command line but as it wasn’t integrated in solutions and projects it only helps you get binaries.

But let’s see how to setup Nuget on mono.

Install & configure Nuget

First download the las exe:

curl -O -L  http://nuget.org/nuget.exe

As Nuget is a .Net binary, you need to launch it via mono. If your installation is ok – and it should with the installer -, you are able to launch mono from every path in your shell. Note also that you need to specify the framework 4 runtime compatibility to make it work properly. So start the nugget exe, just execute this line:

mono --runtime=v4.0 Nuget.exe [then follow your nuget args]

In order to make this work from every, I should advise you to create an executable shell script that handle that.

In the folder you downloaded Nuget, copy it to /usr/local/bin:

cp Nuget.exe /usr/local/bin

Create a new file for your command, just name it nuget to get the same semantics:

touch nuget

Edit your file and just copy/paste this code:

#!/bin/sh
# add a simple 'nuget' command to Mac OS X shell under Mono
PATH=/usr/local/bin:$PATH
mono --runtime=v4.0 /usr/local/bin/NuGet.exe $*

Then give the correct execution rights to your file:

sudo chmod +x nuget

Then open a new console and try nuget (with a nuget help for exemple). The good point is that you can now use Nuget from everywhere!

You should also have a default config path working with NuGet, if you don’t have one created automatically, create a new fresh one

Local config should be stored at:

~/.config/NuGet/Nuget.config

Once you have this, you can play with nuget, with your own repositories too, but you still closed in the command shell.

If you work with MonoDevelop, you have to create your package folder, your packages config file and then reference your binaries manually. It’s not an ideal configuration but that’s better than nohting ;-)

Nuget support on Monodevelop

Install

This morning my eyes flashed after a tweet of David Fowler:

David Fowler tweet about nuget monodevelop support

nuget monodevelop support

Ok, let’s see what is it about?

This is about an Addin written by Matt Ward. You can find the sources on his github 

 

You can build it and install it by your own, but Matt did the things well and provided a Monodevelop addin channel with this:

http://mrward.github.com/monodevelop-nuget-addin-repository/3.0.5/main.mrep

Click on Monodevelop, then Add-in manager. Select Gallery and manage repositories:

monodevelop addin repositories

monodevelop addin repositories

Then click [Add] and paste the url:

Add repository definition url

Add repository definition url

Once you have it, you should get a new addin available:

new addin

new addin

Select it and install.

Let’s try a web project with Nancy!

First, create a new empty ASP.NET web project:

New empty asp.net project

New empty asp.net project

Before playing with Nuget, Add a web.config file because it is not provided by default with this project template:

add web.config

add web.config on monodevelop

Then you should try to run it but obviously you’ll get nothing.

Now, right click on references:

References with Nuget

References with Nuget

So cool! We have now Nuget resources just here! Select Manage Nuget Packages.

You’ll have a box just as the one you use to use under Visual Studio (if you are in click-click mode instead of the Package manager console which is a Powershell host of course…):

Nuget box

Nuget box

Just start with typing ‘Nancy.host’, select ‘Nancy.Hosting.Aspnet’ then Add. You can see in the messages, that it handles correctly the download of Nancy.dll too.

Then add a new class to your project with your HomeModule with a simple test route:

using System;
using Nancy;

namespace Demo
{
	public class HomeModule : NancyModule
	{
		public HomeModule ()
		{
			Get ["/(.*)"] = _ => "Yes it works!";
		}
	}
}

That’s supposed to be enough for now. Build and run. Guess what? it doesn’t work.

Why? Remember that nuget installation features under Visual Studio are full of Powershell and transforming files are part of it. So, remember that when you install something with Monodevelop Nuget you don’t have the transforms like web.config updates!

UPDATE: Thanks to author Matt Ward that points an explication for that in the comments: “PowerShell is not actually needed for the web.config transform to be applied. It is not currently working in the MonoDevelop NuGet addin due to a bug. The addin is not detecting the project is a web project and ignoring the transform. Transforms for app.config files are currently working.”

Then open your web.config file and update  it  to setup Nancy handler:

<?xml version="1.0"?>
<!--
Web.config file for Demo.
The settings that can be used in this file are documented at
http://www.mono-project.com/Config_system.web and

http://msdn2.microsoft.com/en-us/library/b5ysx397.aspx

-->
<configuration>
<system.web>
<compilation defaultLanguage="C#" debug="true" targetFramework="4.0">
<assemblies>
</assemblies>
</compilation>
<customErrors mode="RemoteOnly">
</customErrors>
<authentication mode="None">
</authentication>
<authorization>
<allow users="*" />
</authorization>
<httpHandlers>
<add verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*"/>
</httpHandlers>
<trace enabled="false" localOnly="true" pageOutput="false" requestLimit="10" traceMode="SortByTime" />
<sessionState mode="InProc" cookieless="false" timeout="20" />
<globalization requestEncoding="utf-8" responseEncoding="utf-8" />
<pages>
</pages>
</system.web>
<system.webServer> <modules runAllManagedModulesForAllRequests="true"/> <validation validateIntegratedModeConfiguration="false"/> <handlers> <add name="Nancy" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*"/> </handlers> </system.webServer>
</configuration>

Re-run it and now it works!

How easy to do .Net web!

Just to finish with that and show you how cool it is, relaunch Nuget manager and Add also Nancy.ViewEngines.Razor, jQuery and Twitter bootstrap.

Create Folders Views/Home and add a new html file called “Index.cshtml”

If you’re like me and never remember the right syntax for bootstrapping things, keep a visible bookmark to Twitter bootstrap page and take this one as exemple:

http://twitter.github.com/bootstrap/examples/marketing-narrow.html

Copy paste the source on your file as exemple, then update it to fit your config, paths and update the main block (just extracts with updated things ):

...
<title>Nuget on MonoDevelop</title>
...
<link href="/Content/bootstrap.css" rel="stylesheet">
...
<link href="/Content/bootstrap-responsive.css" rel="stylesheet">
...
<div class="jumbotron">
<h1>Nuget on MonoDevelop!</h1>
<p class="lead">
Get started with .Net Web dev on Mac with Monodevelop, Nuget, Nancy and Bootstrap!
</p>
<p>Generated at @Model.Generated by @Model.Author
<a class="btn btn-large btn-success" href="#">It's alive</a>
</div>

Then update your home module to return the Razor view and with a small model class to test passing things to the view

public class HomeModule : NancyModule
	{
		public HomeModule ()
		{
		Get ["/(.*)"] = _ =>
                   View ["Index", new HomeInfo (
                          DateTime.Now, "Rui Carvalho")];
		}
	}

	public class HomeInfo
	{
		public DateTime Generated { get; private set; }
		public string Author { get; private set; }

		public HomeInfo (DateTime generated, string author)
		{
			Generated = generated;
			Author = author;
		}
	}

Run it and enjoy more happy dappy path of web development with Nancy, Nuget and Mono on your Mac

Nancy with twitter bootstrap

Nancy with twitter bootstrap

cheers!

 

Posted in Articles Tagged with: , , , ,
4 comments on “.Net Web dev Mac – Nuget, Monodevelop and Nancy
  1. Jef Claes says:

    Very nice work! Another reason gone not to buy a Mac ;)

  2. Matt Ward says:

    PowerShell is not actually needed for the web.config transform to be applied. It is not currently working in the MonoDevelop NuGet addin due to a bug. The addin is not detecting the project is a web project and ignoring the transform. Transforms for app.config files are currently working.

  3. sounds very great ! I will try on my ubuntu :) Another reason not to buy not free system :p

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>