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. IOS开发创建开发证书及发布App应用(七)——在iTunes创建填写应用基本信息

    7.在iTunes创建填写应用基本信息 依旧打开苹果的开发者网站 https://developer.apple.com/ 点击Member,如下图 (注意,下面的图示是登录之后的) 点击iTunes ...

  2. 自动生成并导出word文档

    今天很荣幸又破解一现实难题:自动生成并导出word文档 先看页面效果: word效果: 代码: 先搭建struts2项目 创建action,并在struts.xml完成注册 <?xml vers ...

  3. Dubbo的配置及使用

    1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需 ...

  4. 【Ubuntu】您没有查看“sf_VirtualDisk”的内容所需的权限。

    原文链接:http://www.crifan.com/can_not_access_share_folder_in_ubuntu_virtualbox/ [问题] 之前已经搞定可以自动共享文件夹了: ...

  5. WebService基础学习(二)—三要素

    一.Java中WebService规范      JAVA 中共有三种WebService 规范,分别是JAX-WS.JAX-RS.JAXM&SAAJ(废弃).   1.JAX-WS规范    ...

  6. [编织消息框架][JAVA核心技术]动态代理应用8-IRpcReceive实现

    private static Map<Short, Map<Byte, Method>> RECEIVE_METHOD_INFO = new HashMap<>() ...

  7. 使用HttpClient进行Get方式通信

    下载apache包 http://hc.apache.org/downloads.cgi 比较eclipse自带api,简单,易上手 实例: package zw1; import java.io.I ...

  8. sublime text 3 打造舒适黑色主题

    效果: 这里我使用了两个主题插件组合成的 Spacegray Afterglow Ctrl+Shift+P -> Package Control:Install Packages 分别输入Spa ...

  9. xmlplus 组件设计系列之一 - 图标

    网页上使用的图标分可为三种:文件图标.字体图标和 SVG 图标.对于文件图标,下面仅以 PNG 格式来说明. PNG 图标 对于 PNG 图标的引用,有两种方式.一种是直接由 HTML 元素 img ...

  10. jQuery常用代码片段

    检测IE浏览器 在进行CSS设计时,IE浏览器对开发者及设计师而言无疑是个麻烦.尽管IE6的黑暗时代已经过去,IE浏览器家族的人气亦在不断下滑,但我们仍然有必要对其进行检测.当然,以下片段亦可用于检测 ...