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>
然后看看运行结果
谢谢欣赏!