NancyFx框架的自定义模块

新建一个空的Web项目

然后通过NuGet库安装下面的包

  • Nancy
  • Nancy.Hosting.Aspnet

然后添加Models,Module,Views三个文件夹,并在Models文件里面添加NancyRouteAttribute类

        //路由的方法
public string Method { get; set; }
//路由的路径
public string Path { get; set; }
public NancyRouteAttribute(string method,string path)
{
this.Method = method;
this.Path = path;
}

然后在Module文件夹添加UglifiedNancyModule类

        //使用的自定义 INancyModule 实现
//方法的属性(eugh!) 来定义路由。
//没有人在他们正确的头脑将编写一个网络框架
//使用属性进行路由的;
public AfterPipeline After { get; set; }
public BeforePipeline Before { get; set; }
public ErrorPipeline OnError { get; set; }
public NancyContext Context { get; set; }
public IResponseFormatter Response { get; set; }
public IModelBinderLocator ModelBinderLocator { get; set; }
public ModelValidationResult ModelValidationoResult { get; set; }
public IModelValidatorLocator ValidatorLocator { get; set; }
public Request Request { get; set; }
public IViewFactory ViewFactory { get; set; }
public string ModulePath { get; set; }
public ViewRenderer View { get { return new ViewRenderer(this); } }
public Negotiator Negotiate { get { return new Negotiator(this.Context); } }
public UglifiedNancyModule():this(string.Empty)
{ }
public IEnumerable<Route> Routes
{
get { return this.GetRoutes(); }
}
public dynamic Text { get; set; }
private UglifiedNancyModule(string modulePath)
{
this.After = new AfterPipeline();
this.Before = new BeforePipeline();
this.OnError = new ErrorPipeline();
this.ModulePath = modulePath;
}
//在类上运行所有方法
//为我们的属性。如果我们是为了一个真实的
//我们将检查参数和返回类型等
private IEnumerable<Route> GetRoutes()
{
var routes = new List<Route>();
var type = this.GetType();
var methods = type.GetMethods(BindingFlags.Instance|BindingFlags.Public);
foreach (var method in methods)
{
var attribute = method.GetCustomAttributes(typeof(NancyRouteAttribute),false).FirstOrDefault() as NancyRouteAttribute;
if (attribute==null)
{
continue;
}
var routeDelegate = WrapFunc((Func<dynamic,dynamic>)Delegate.CreateDelegate(typeof(Func<dynamic,dynamic>),this,method.Name));
var filter = this.GetFilter(method.Name);
var fullPath = string.Concat(this.ModulePath,attribute.Path);
routes.Add(new Route<object> (attribute.Method.ToUpper(),fullPath,filter,routeDelegate));
}
return routes.AsReadOnly();
} //在返回任务的委托中包装同步委托
private Func<NancyContext, bool> GetFilter(string routeMethodName)
{
var type = this.GetType();
var method = type.GetMethod(routeMethodName+"Filter",BindingFlags.Public|BindingFlags.Instance);
if (method==null)
{
return null;
}
return (Func<NancyContext,bool>)Delegate.CreateDelegate(typeof(Func<NancyContext,bool>,this,method.Name));
}
private static Func<dynamic,CancellationToken,Task<dynamic>> WrapFunc(Func<object,object> syncFunc)
{
return(p,ct) =>
{
var tcs = new TaskCompletionSource<dynamic>();
try
{
var result = syncFunc.Invoke(p);
tcs.SetResult(result);
}
catch (Exception e)
{
tcs.SetException(e);
//throw;
}
return tcs.Task;
};
}

继续在Module文件夹添加MainModule类

        [NancyRoute("GET", "/")]
public dynamic Root(dynamic parameters)
{
return View["Index", new { Name = "Lexan!" }];
} public bool FilteredFilter(NancyContext context)
{
return false;
} [NancyRoute("GET", "/filtered")]
public dynamic Filtered(dynamic parameters)
{
return "筛选";
}

 然后往根目录添加Bootstrapper类

  public override void Configure(INancyEnvironment environment)
{
//base.Configure(environment);
environment.Diagnostics(enabled:true,password:"password");
}

继续往根目录添加SharedAssemblyInfo类

using System.Runtime.InteropServices;
using System.Reflection; [assembly:AssemblyInformationalVersion("2.0.0-alpha")]

继续往Views文件夹里面添加index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>你好!</title>
</head>
<body>
<h1>你好 @Model.Name 这是使用自定义模块类型实现的</h1>
</body>
</html>

然后看看运行结果

谢谢欣赏!

NancyFx 2.0的开源框架的使用-CustomModule(自定义模块)的更多相关文章

  1. NancyFx 2.0的开源框架的使用-Basic

    这是NancyFx开源框架中的Basic认证,学习一下! 首先当然是新建一个空的Web,BasicDemo 继续在项目中添加Nuget包,记得安装的Nuget包是最新的预发行版 Nancy Nancy ...

  2. NancyFx 2.0的开源框架的使用-ModelBinding(实现绑定)

    NancyFx框架中使用绑定模型 新建一个空的Web程序 然后安装Nuget库里面的包 Nancy Nancy.Hosting.Aspnet Nancy.ViewEnglines.Spark 并在We ...

  3. NancyFx 2.0的开源框架的使用-HosingOwin

    Nancy框架的Owin使用 先建一个空的Web项目 然后往Nuget库里面添加Nancy包 Nancy Nancy.Owin Nancy.ViewEnglines.Spark 然后添加Models, ...

  4. NancyFx 2.0的开源框架的使用-Authentication

    新建一个空的项目 新建好了空的项目以后,接着通过NuGet安装一下三个包 Nancy Nancy.Hosting.Aspnet Nancy.ViewEnglines.Razor 然后在项目中添加Mod ...

  5. NancyFx 2.0的开源框架的使用-Forms

    同样的像前面2篇博文一样,每个项目的开始基本都是建个空的Web项目 在NuGet库中安装以下几个NuGet包 Nancy Nancy.Authentication.Forms Nancy.Hostin ...

  6. NancyFx 2.0的开源框架的使用-Stateless

    同样和前面一样新建一个空的Web项目,都在根目录添加Module,Models,Views文件夹 添加Nuget包 在Models文件夹里面添加UserModel类 public string Use ...

  7. NancyFx 2.0的开源框架的使用-Stateless(二)

    继续上一篇Stateless的博文,在上一篇的博文的基础上稍微加点东西 接下来右键解决方案添加新项目,一样建一个空的Web项目 然后在StatelessDemoWeb项目里面添加Views文件夹,Sc ...

  8. NancyFx 2.0的开源框架的使用-AspnetBootstrapping

    新建一个空的Web项目AspnetBootstrappingDemo 然后添加NuGet组件 Nancy Nancy.Hosting.Aspnet Nancy.ViewEngines.Razor 继续 ...

  9. NancyFx 2.0的开源框架的使用-Caching

    新建一个空的Web项目,命名CachingDemo 然后添加三个Nuget安装包 Nancy Nancy.Hosting.Aspnet Nancy.ViewsEngines.Razor 然后往项目里面 ...

随机推荐

  1. JS+CSS实现的下拉刷新/上拉加载插件

    闲来无事,写了一个当下比较常见的下拉刷新/上拉加载的jquery插件,代码记录在这里,有兴趣将代码写成插件与npm包可以留言. 体验地址:http://owenliang.github.io/pull ...

  2. Snapman开发接口

    #include "stdafx.h" #include <Windows.h> #include <string> #include<time.h& ...

  3. python 实例方法,类方法和静态方法

    在学习python代码时,看到有的类的方法中第一参数是cls,有的是self,经过了解得知,python并没有对类中方法的第一个参数名字做限制,可以是self,也可以是cls,不过根据人们的惯用用法, ...

  4. stm32串口通讯问题

    stm32串口通讯问题 在串口试验中,串口通讯不正常,则可能会出现以下问题: 1. 配置完成后,串口没有任何消息打印. 原因:1,端口配置有问题,需要重新检查I/O口的配置 2,接线有问题,检查接线是 ...

  5. DataTable 转实体

    因为Linq的查询功能很强大,所以从数据库中拿到的数据为了处理方便,我都会转换成实体集合List<T>. 开始用的是硬编码的方式,好理解,但通用性极低,下面是控件台中的代码: using ...

  6. iTunes制作iPhone手机铃声方法(mac版2017年4月更新)

    iTunes制作iPhone手机铃声方法(mac版2017年4月更新)   跟之前百度出来的不同,我自己使用后写的.     1.首先下载好你需要制作铃声的mp3文件,这里我放在桌面.       2 ...

  7. 多云时代,海外微软Azure云与国内阿里云专线打通性能测试

    本文地址:http://www.cnblogs.com/taosha/p/6528730.html 在云计算的大时代,大型客户都有业务全球拓展的需求,考虑到成本,时间因素,一般都是选择云计算,现在云计 ...

  8. struts2 之 ThreadLocal 和 ActionContext

    1. ThreadLocal:该类提供了线程局部(thtead-local)变量.threadLocal是一个容器,该容器中存放的数据可以保证线程安全. 案例如: public class Threa ...

  9. iOS多线程的三种方法

    前言 在多线程简介中,我已经说明过了,为了提高界面的流畅度以及用户体验.我们务必要把耗时的操作放到别的线程中去执行,千万不要阻塞主线程.iOS中有以下3种多线程编程方法: NSThread Grand ...

  10. android参数传递的几种方法

    Intent Intent i=new Intent(当前Activity.this,目标Activity.class); 1.传单值 传入: i. i.putExtra("名称" ...