Nano is a micro web framework for building web-based HTTP services and websites for .NET.


Keywords
service, web, static, Nano, self, self-host, websites, httplistener, c#, framework, asp.net, micro, host, website, webservice, selfhost, api
License
MIT
Install
Install-Package Nano -Version 1.0.0

Documentation

Nano Logo

Windows Linux NuGet Azure Demo Website
windows linux NuGet Version Azure Status

Nano is a .NET cross-platform micro web framework for building web-based HTTP services and websites.

Features

  • Rapidly create web APIs by writing static methods
  • Run self-hosted or within an ASP.NET website
  • Add it as a C# single file drop in or referenced as a NuGet Package
  • Easy setup and low ceremony so that you can focus on business logic and not infrastructure and framework hassles
  • Api Explorer webpage saves you massive time!
    • Enables exploring and invoking available endpoints and operations
    • Displays XML source code documentation
    • Enables rapid testing and doesn't require QA analysts to use external tools to invoke your web service
    • Can serve as a "free" auto-generated UI that can be given to internal users
    • Includes a built-in C# and JavaScript proxy generator which enables clients to generate a proxy to call your API
  • Easy to use extension points for intercepting requests, responses, or errors in order to implement logging, authentication, authorization, etc.
  • Allows developers to simply write static method and have them automatically be exposed as JSON endpoints at a convention based URL. As fast as you can write a method is as fast as you can have an API.
  • Ability to go low level by using the NanoContext class in the instance that you need to control content type, return blobs, handles headers or cookies, etc.

Demo

The Nano demo website and Api Explorer is hosted on Azure and can be viewed here.

There are also four different demo projects included demonstrating how to use Nano as a self-hosed console application, a self-hosted Windows service, a self-hosted Topshelf managed Windows service, or within an ASP.NET application.

Getting Started

Follow this five minute introduction to create your first self-hosted Nano application.

Step One - Create a new solution and a C# console application project called "NanoIsAwesome"

Step Two - Install Newtonsoft.Json from Nuget

Step Three - Download the single-file drop-ins for both Nano nano.cs and the Api Explorer index.html and add them to your project structure which should look like this:

> NanoIsAwesome
  > Properties
  > References
  > www
    > ApiExplorer
        index.html
    App.config
    HelloWorldApi.cs
    Nano.cs
    packages.config
    Program.cs

Step Four - Create a new class in the root of your project named "HelloWorldApi". Then just copypasta the code below:

namespace NanoIsAwesome
{
    class HelloWorldApi
    {
        public static string HelloWorld()
        {
            return "Hello World!";
        }
    }
}

Step Five - Update your Program.cs Main method to configure and start Nano. Just copy the code below. Feel free to read the comments if you’re curious about what each piece does:

static void Main(string[] args)
{
    // Create a new configuration instance for Nano
    var config = new Nano.Web.Core.NanoConfiguration();

    // This exposes all public static methods in the HelloWorldApi class as JSON endpoints
    config.AddMethods<HelloWorldApi>();

    // Map the www directory so it can be served. This allows us to access the Api Explorer
    // by browsing to the ApiExplorer sub-directory
    if (Debugger.IsAttached)
    {
        // If we're debugging, we need to map the directory as it appears in the project
        config.AddDirectory("/", "../../www", returnHttp404WhenFileWasNotFound: true);
    }
    else
    {
        // If we're not debugging, we can map the directory as it will appear in the deployed code
        config.AddDirectory("/", "www", returnHttp404WhenFileWasNotFound: true);
    }

    // Start the Nano server using the config and tell it to run at the given URL
    using (var server = Nano.Web.Core.Host.HttpListener.HttpListenerNanoServer.Start(config, "http://localhost:4545"))
    {
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}

Step Six - Start the debugger and navigate to http://localhost:4545/ApiExplorer/ to use the built-in testing endpoint or http://localhost:4545/Api/HelloWorldApi/HelloWorld to hit the endpoint directly.

And that’s it. You're successfully using Nano to serve a website and an API endpoint. Visit the Getting Started Guide, the Nano Wiki, or browse the source code, unit tests, and demo projects to learn more.

Api Explorer

The Api Explorer page is an integral part of the Nano experience as it allows you to interact with any of your web service endpoints with ease. Here is a quick list of features:

  • Built-in C# and JavaScript proxy generator
  • Displays method (operation) names, URLs, data types, and method and parameter XML comments
  • Ability to search by API classes or methods
  • Ability to invoke methods
  • Displays API results along with HTTP status code and execution time
  • Displays sample JSON input for methods that take complex objects

Api Explorer Screenshot

The Nano demo website and Api Explorer is hosted on Azure and can be viewed here.

Contributors

Thank you to all of the contributors who have helped shaped Nano in big and small ways:

Thank You

Thanks to the following companies for providing free resources to help this maintain this project:

  • AppVeyor - Continuous integration build server for running our Windows tests
  • Travis CI - Continuous integration build server for running our Linux (Mono) tests
  • Microsoft Azure - Web hosting

License

Nano is open source licensed under the terms of the MIT License and available for free. See this projects LICENSE file for more information.

Acknowledgments

Nano made use of substantial portions and/or was heavily influenced by the following open source software:

  • Nancy: https://github.com/NancyFx/Nancy

    The MIT License
    Copyright (c) 2010 Andreas HĂĄkansson, Steven Robbins and contributors
    License available at: https://github.com/NancyFx/Nancy/blob/master/license.txt
    
  • Katana Project: http://katanaproject.codeplex.com/

    Apache License
    Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
    License available at: http://katanaproject.codeplex.com/SourceControl/latest#LICENSE.txt
    
  • DynamicDictionary: https://github.com/randyburden/DynamicDictionary

    The MIT License
    Copyright (c) 2014 Randy Burden ( http://randyburden.com ) All rights reserved.
    License available at: https://github.com/randyburden/DynamicDictionary/blob/master/LICENSE
    
  • JSON.NET: https://github.com/JamesNK/Newtonsoft.Json

    The MIT License
    Copyright (c) 2007 James Newton-King
    License available at: https://github.com/JamesNK/Newtonsoft.Json/blob/master/LICENSE.md
    

Links