Nancy是一个轻量级的MVC框架,也可以完全脱离IIS.
下面是官网的一些介绍:
Nancy 是一个轻量级用于构建基于 HTTP 的 Web 服务,基于 .NET 和 Mono 平台,框架的目标是保持尽可能多的方式,并提供一个super-duper-happy-path所有交互。
Nancy 设计用于处理 DELETE, GET, HEAD, OPTIONS, POST, PUT和 PATCH 等请求方法,并提供简单优雅的 DSL 以返回响应。让你有更多时间专注于你的代码和程序。
官方地址:http://nancyfx.org
GitHub:https://github.com/NancyFx/Nancy
Nancy的模块的概念类似于Asp.net中的Controller,
一个典型的示例如下:
public class Module : NancyModule
{
public Module()
{
Get["/greet"] = x => "hello world";
}
}父路由:
public class ResourceModule : NancyModule
{
public ResourceModule() : base("/products")
{
//此时的路径就是 /products/list
Get["/list"] = _ => "The list of products";
}
}参数:
Get["/greet/{name}"] = para => $"Hello {para.name}";QueryString传递参数:
public class Module : NancyModule
{
public Module()
{
Get["/greet"] = para =>
{
var name = Request.Query["name"];
return $"Hello {name}";
};
}
}使用方法 具体的Demo:
一 新建一个 控制台程序:

二、使用 NuGet 安装所需包

三、编译代码监听端口:
using Nancy.Hosting.Self;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NancyDemo01
{
class Program
{
static void Main(string[] args)
{
var _url = "http://127.0.0.1";
var _port = 7880;
var host = new NancyHost(new Uri($"{_url}:{_port}"));
host.Start();
Console.WriteLine("系统启动 监听地址:");
Console.WriteLine($"{_url}:{_port}");
Console.Read();
host.Stop();
}
}
}写一个测试的Module
using Nancy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NancyDemo01
{
public class Module : NancyModule
{
public Module()
{
///
Get("/hello", x => {
return "Hello World";
});
//传参
Get("/user/{id:int}", parameter =>
{
var person= new Person
{
Name = "测试",
Age = parameter.id
};
return Response.AsJson(person);
});
}
}
}运行程序:

访问地址:127.0.0.1:7880/hello

其他说明:由于系统要监听端口,所以用管理员权限运行。