1. 程式人生 > >NancyFx 2.0的開源框架的使用-CustomModule(自定義模塊)

NancyFx 2.0的開源框架的使用-CustomModule(自定義模塊)

nuget eat ews pub 技術分享 continue for eth color

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 = "Jimbo!" }];
        }

        public bool FilteredFilter(NancyContext context)
        {
            return false;
        }

        [NancyRoute("GET", "/filtered")]
        public dynamic Filtered(dynamic parameters)
        {
            return "篩選";
        }

技術分享

NancyFx 2.0的開源框架的使用-CustomModule(自定義模塊)