首页 > 代码库 > NancyFx 2.0的开源框架的使用-CustomModule(自定义模块)
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 one rror { 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 = "Jimbo!" }]; } public bool FilteredFilter(NancyContext context) { return false; } [NancyRoute("GET", "/filtered")] public dynamic Filtered(dynamic parameters) { return "筛选"; }
NancyFx 2.0的开源框架的使用-CustomModule(自定义模块)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。