NancyFx 2.0的开源框架的使用-CustomModule(自定义模块)
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(自定义模块)的更多相关文章
- NancyFx 2.0的开源框架的使用-Basic
这是NancyFx开源框架中的Basic认证,学习一下! 首先当然是新建一个空的Web,BasicDemo 继续在项目中添加Nuget包,记得安装的Nuget包是最新的预发行版 Nancy Nancy ...
- NancyFx 2.0的开源框架的使用-ModelBinding(实现绑定)
NancyFx框架中使用绑定模型 新建一个空的Web程序 然后安装Nuget库里面的包 Nancy Nancy.Hosting.Aspnet Nancy.ViewEnglines.Spark 并在We ...
- NancyFx 2.0的开源框架的使用-HosingOwin
Nancy框架的Owin使用 先建一个空的Web项目 然后往Nuget库里面添加Nancy包 Nancy Nancy.Owin Nancy.ViewEnglines.Spark 然后添加Models, ...
- NancyFx 2.0的开源框架的使用-Authentication
新建一个空的项目 新建好了空的项目以后,接着通过NuGet安装一下三个包 Nancy Nancy.Hosting.Aspnet Nancy.ViewEnglines.Razor 然后在项目中添加Mod ...
- NancyFx 2.0的开源框架的使用-Forms
同样的像前面2篇博文一样,每个项目的开始基本都是建个空的Web项目 在NuGet库中安装以下几个NuGet包 Nancy Nancy.Authentication.Forms Nancy.Hostin ...
- NancyFx 2.0的开源框架的使用-Stateless
同样和前面一样新建一个空的Web项目,都在根目录添加Module,Models,Views文件夹 添加Nuget包 在Models文件夹里面添加UserModel类 public string Use ...
- NancyFx 2.0的开源框架的使用-Stateless(二)
继续上一篇Stateless的博文,在上一篇的博文的基础上稍微加点东西 接下来右键解决方案添加新项目,一样建一个空的Web项目 然后在StatelessDemoWeb项目里面添加Views文件夹,Sc ...
- NancyFx 2.0的开源框架的使用-AspnetBootstrapping
新建一个空的Web项目AspnetBootstrappingDemo 然后添加NuGet组件 Nancy Nancy.Hosting.Aspnet Nancy.ViewEngines.Razor 继续 ...
- NancyFx 2.0的开源框架的使用-Caching
新建一个空的Web项目,命名CachingDemo 然后添加三个Nuget安装包 Nancy Nancy.Hosting.Aspnet Nancy.ViewsEngines.Razor 然后往项目里面 ...
随机推荐
- 老李分享:curl发起https请求
老李分享:curl发起https请求 在POPTEST上课的过程中,我们需要本地模拟https请求来完成性能测试,我们用curl来实现,curl是利用URL语法在命令行方式下工作的开源文件传输工具,使 ...
- iptables初探
一,前言 本来想起个名字叫做"小白都是怎么学习iptables的?"或者"你为什么还不了解iptables?"等等,就像简书上的头条文章,虽然被说成" ...
- 《ECMAScript标准入门》第二版读书笔记
title: <ECMAScript标准入门>第二版 date: 2017-04-10 tags: JavaScript categories: Reading-note 2015年6月, ...
- mysql 分析2 show processlist ;
show processlist ; 可以查看当前有哪些链接 处于什么状态 分析语句 那些连接处于什么状态 (需要通过脚本观察一段时间内的有运行情况做出统计一直刷新服务器运行状态 ) 当出现下面的几种 ...
- HBase应用快速学习
HBase是一个高性能.面向列.可伸缩的开源分布式NoSQL数据库,是Google Bigtable的开源实现. HBase的思想和应用和传统的RDBMS,NoSQL等有比较大的区别,这篇文章从HBa ...
- less学习笔记(一)
less的写法如下 .content { ul{ list-style: none; } li{ height: 25px; line-height: 25px; padding-left: 15px ...
- File类遍历目录及文件
1. 构造函数 File(String args0)//使用一个表示文件或目录的路径的字符串创建一个File对象 File(URL args0)//使用一个URL对象创建File对象 File(Fil ...
- 从零到实现Shiro中Authorization和Authentication的缓存
本文大纲 一.简介 二.缓存的概念 三.自定义实现缓存机制 四.什么是Ehcache 五.Ehcache怎么用 六.Spring对缓存的支持 七.Spring+Ehcache实现 八.Spring+S ...
- nginx源码编译问题
[root@localhost nginx-1.7.4]# ./configure checking for OS + Linux 2.6.32-431.el6.x86_64 x86_64 check ...
- StringBuilder的实现
先看看MS给出的官方解释吧 (http://msdn.microsoft.com/zh-cn/library/system.text.stringbuilder(VS.80).aspx) String ...