代码之家  ›  专栏  ›  技术社区  ›  Xpleria Dholu

如何将Func<>属性/方法传递给NancyFx Get()方法?

  •  1
  • Xpleria Dholu  · 技术社区  · 6 年前

    南希(2。x) NancyModule.Get() 方法定义为:

    public virtual void Get(string path, Func<dynamic, object> action, [Func<NancyContext, bool> condition = null], [string name = null]);
    

    通常的用法是:

    public class MyModule
    {
        public MyModule() 
        {
            this.Get("/", parameters => {
                this.RequestHandler = new RequestHandler();
    
                return this.RequestHandler.HandleRequest("/", parameters, someOtherInfo);
            });
        }
    }
    

    我想将第二个参数定义为一个属性,这样我就可以使用以下几种路径:

    public class MyModule
    {
        Func<dynamic, object> indexHandler = parameters => {
            // Error: Keyword "this" is not available in this context
            this.RequestHandler = new RequestHandler();
    
            // Error: Keyword "this" is not available in this context
            return this.RequestHandler.HandleRequest("/", parameters, someOtherInfo);
        };
    
        public MyModule() 
        {
            this.Get("/", indexHandler);
            this.Get("/index", indexHandler);
        }
    }
    

    如果我这样做,它会起作用:

    public class MyModule
    {
        public MyModule() 
        {
            Func<dynamic, object> indexHandler = parameters => {
                this.RequestHandler = new RequestHandler();
    
                return this.RequestHandler.HandleRequest("/", parameters, someOtherInfo);
            };
    
            this.Get("/", indexHandler);
            this.Get("/index", indexHandler);
        }
    }
    

    但我不想在构造函数中定义它。我做错什么了?还有别的办法吗?

    MVCE公司

    依赖包: 南希(版本:2.0.0-clinteastwood)

    using Nancy;
    using Nancy.Responses.Negotiation;
    
    namespace MyNamespace
    {
        public class MyModule : NancyModule
        {
            private RequestHandler RequestHandler;
            private object IndexHandler(dynamic parameters)
            {
                this.RequestHandler = new RequestHandler();
                var someOtherInfo = "";
                return this.RequestHandler.HandleRequest("/", parameters, someOtherInfo);
            }
    
            public MyModule()
            {
                this.Get("/", IndexHandler);
                this.Get("/index", IndexHandler);
    
                this.Get("/home", parameters => {
                    return this.Negotiate.WithView("myview.html");
                });
            }
        }
    
        public class RequestHandler
        {
            public Negotiator HandleRequest(string path, dynamic parameters, string someOtherInfo)
            {
                return new Negotiator(new NancyContext());
            }
        }
    }
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   Haytam    6 年前

    安得烈的回答是有效的,应该已经足够了,但是显然(在MVCE中)方法定义不存在。

    下面是正确的定义(至少是VS想要的定义):

    public virtual void Get(string path, Func<object, Task<object>> action, Func<NancyContext, bool> condition = null, string name = null)
    

    幸运的是你 HandleRequest 是可等待的,因此您只需要编辑返回类型。
    以下是正确的定义:

    private Task<object> IndexHandler(dynamic parameters)
    {
        this.RequestHandler = new RequestHandler();
        var someOtherInfo = "";
        return this.RequestHandler.HandleRequest("/", parameters, someOtherInfo);
    }
    

    希望这有帮助!

        2
  •  4
  •   Andrei Tătar    6 年前

    这应该有效:

    public class MyModule
    {
        public MyModule() 
        {
           this.Get("/", IndexHandler);
           this.Get("/index", IndexHandler);
        }
    
        private object IndexHandler(dynamic parameters) {
            this.RequestHandler = new RequestHandler();
            return this.RequestHandler.HandleRequest("/", parameters, someOtherInfo);
        }
    }