Getting started with nancy
Remarks#
About:
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.
Additional Resources:
- Main website: https://nancyfx.org
- Documentation: https://github.com/NancyFx/Nancy/wiki
- Source Code: https://github.com/NancyFx/Nancy
- Nuget Package: https://www.nuget.org/packages/Nancy/
Versions#
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 |
Create a simple self-hosted Nancy application
- Use Nuget to install the Nancy and Nancy.Hosting.Self packages into the project.
- Instantiate a new NancyHost object and pass in the relevant URL
using( var host = new NancyHost( hostConfiguration, new Uri( "https://localhost:1234" ) ) ) { host.Start(); Console.WriteLine( "Running on https://localhost:1234" ); Console.ReadLine(); }
Place this code in your project at the point when you wish to start listening for http traffic. 3. Add a class to your project that inherits from NancyModule and add a constructor method.
public class FooModule : NancyModule
{
public FooModule()
{
}
}
-
Define routes in the constructor:
… public FooModule() { Get[“Bar”] = parameters => { return “You have reached the /bar route”; } }
Setup Nancyfx with Dotnet core v1.1, Kestrel, and Visual Studio Code on *nix systems
Prerequiste steps:
- Get dotnet core for your platform:
- Follow instructions and make sure dotnet core is working
- Get Visual Studio Code for your platform:
- Launch Visual Studio Code (VS code) and install the C# extension then reload
Create self hosted NancyFx project:
-
Setup a project with a correct project directory structure.
Open Bash Terminal and type:
mkdir nancydotnetcore cd nancydotnetcore mkdir src mkdir test touch global.json
-
Open global.json and enter the following code:
{ "projects":["src", "test"] }
-
In Bash terminal:
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.
-
Add the dependencies, open "project.json" and overwrite it with the following:
{ "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”
-
Create folder "Modules" in VSCode project
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!"); } } }
-
In the root directory of the project create a file called "Startup.cs" and copy and paste the following:
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(); } } }
-
Done! Now lets run this and see the output.
Click the debug symbol in VS Code, and Click the run button. It should compile and start the project.
Open the browser @ https://localhost:5000
- Pat yourself on the back and enjoy!