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. MongoDB基础教程系列--第一篇 进入MongoDB世界

    1.什么是MongoDB MongoDB是跨平台的.一个基于分布式文件存储的数据库.由C++语言编写.用它创建的数据库具备性能高.可用性强.易于扩展等特点.MongoDB将数据存储为一个文档,数据结构 ...

  2. Visual Studio 2017无法加载Visual Studio 2015创建的SharePoint解决方案

    前几天安装了最新的Visual Studio 2017企业版,发现无法打开之前使用Visual Studio 2015创建的SharePoint 2016解决方案,提示"需要更新" ...

  3. PCIE_DMA实例三:Xilinx 7系列(KC705/VC709)FPGA的EDK仿真

    一:前言 好久没写博客了,前段时间有个朋友加微信请教关于PCIe的一些学习方法.本人也不是专家,只是略知一些皮毛.对于大家反馈的问题未必能一一解答,但一定知无不言.以后我会常来博客园看看,大家可以把问 ...

  4. SpringCloud网关ZUUL集成consul

    最近一直在搞基于springcloud的微服务开发,为了不限定微服务开发语言,服务发现决定采用consul不多说上代码 pom文件 <project xmlns="http://mav ...

  5. iOS实现高斯模糊效果(Swift版本)

    给UIimage添加分类 extension UIImage { /// 高斯模糊 func gaussianBlur(var blurAmount:CGFloat) -> UIImage { ...

  6. 快速排序算法回顾 --冒泡排序Bubble Sort和快速排序Quick Sort(Python实现)

    冒泡排序的过程是首先将第一个记录的关键字和第二个记录的关键字进行比较,若为逆序,则将两个记录交换,然后比较第二个记录和第三个记录的关键字.以此类推,直至第n-1个记录和第n个记录的关键字进行过比较为止 ...

  7. Redis技术分享

    环境介绍: 开发环境: spring3+tomcat7+maven3+redis-3.0.7 运行环境: Linux 前言: 项目中引入redis背景: 项目中最初将科目.打印.利润表.资产负债表.现 ...

  8. Executor框架学习笔记

    Java中的线程即是工作单元也是执行机制,从JDK 5后,工作单元与执行机制被分离.工作单元包括Runnable和Callable,执行机制由JDK 5中增加的java.util.concurrent ...

  9. commitProperties方法

    自定义的组件,如果重写commitProperties方法,那么在该方法内部一定要注意super.commitProperties()的调用.

  10. DirectFB学习笔记一

    本文记录directfb程序的基本操作流程. 1.首先创建一个directfb对象:DirectFBInit(&argc,&argv)初始化然后创建DirectFBCreate(&am ...