Nancy is a lightweight framework for building HTTP based services in .Net based off of the Sinatra framework that exists for Ruby. It is designed to allow for handling of several different types of HTTP Requests and provides a simple way to return a response with a small amount of code.
Version | Release Date |
---|---|
Nancy 1.4.3 | 2015-12-21 |
Nancy 1.4.2 | 2015-11-23 |
Nancy 1.4.1 | 2015-11-05 |
Nancy 1.4.0 | 2015-10-29 |
Nancy 1.3.0 | 2015-09-25 |
Nancy 1.2.0 | 2015-04-17 |
Nancy 1.1.0 | 2015-02-18 |
Nancy 1.0.0 | 2015-01-23 |
using( var host = new NancyHost( hostConfiguration, new Uri( "http://localhost:1234" ) ) ) { host.Start(); Console.WriteLine( "Running on http://localhost:1234" ); Console.ReadLine(); }
Place this code in your project at the point when you wish to start listening for http traffic.
public class FooModule : NancyModule
{
public FooModule()
{
}
}
...
public FooModule()
{
Get["Bar"] = parameters => {
return "You have reached the /bar route";
}
}
Open Bash Terminal and type:
mkdir nancydotnetcore cd nancydotnetcore mkdir src mkdir test touch global.json
{ "projects":["src", "test"] }
cd src mkdir NancyProject1 dotnet new
Open folder NancyProject1 in VS code
You will get a warning: "Required assets to build and debug are missing from 'nancyproject1'."
Click "Yes"
Also you will see: There are unresolved dependencies from 'project.json'. Please execute the restore command to continue.
Click "Close" we will get to this soon.
{ "version": "1.0.0-*", "buildOptions": { "debugType": "portable", "emitEntryPoint": true }, "frameworks": { "netcoreapp1.1": { "dependencies": { "Microsoft.AspNetCore.Hosting": "1.1.0", "Microsoft.AspNetCore.Server.Kestrel": "1.1.0", "Microsoft.AspNetCore.Owin": "1.1.0", "Nancy": "2.0.0-barneyrubble", "Microsoft.NETCore.App": { "type": "platform", "version": "1.1.0" } } } } }
VS code will ask to restore click "Restore"
In the Modules folder add a file named "IndexModule.cs" then copy and save the following:
namespace NancyProject1 { using Nancy; public class IndexModule : NancyModule { public IndexModule() { Get("/", _ => "Hello dotnet core world!"); } } }
namespace NancyProject1 { using Microsoft.AspNetCore.Builder; using Nancy.Owin; public class Startup { public void Configure(IApplicationBuilder app) { app.UseOwin(x => x.UseNancy()); } } }
Open file "Program.cs" and overwrite the content with the following and save:
namespace NancyProject1
{
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseKestrel()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
}
Click the debug symbol in VS Code, and Click the run button. It should compile and start the project.
Open the browser @ http://localhost:5000