没有 lambda 表达式的路由

Route without lambda expression

下面是the official example for registering routes in Nancy。但是,如果我不想在该方法中 "do something",而是在 DoSomething() 中执行怎么办?

public class ProductsModule : NancyModule
{
    public ProductsModule()
    {
        Get["/products/{id}"] = _ =>
        {
            //do something
        };
    }
}

public abstract class NancyModule : INancyModule, IHideObjectMembers
{
    public RouteBuilder Get { get; }
}

public class RouteBuilder : IHideObjectMembers
{
    public RouteBuilder(string method, NancyModule parentModule);
    public Func<dynamic, dynamic> this[string path] { set; }
}

我不知道 DoSomething 应该有什么签名。这可以像下面这样工作吗?并不是说我不能使用 lambda 表达式;我很好奇,因为 Nancy 使用的所有这些模式看起来都非常奇怪和独特。

public class ProductsModule : NancyModule
{
    ???? DoSomething(????)
    {
        //do something
        return ????
    }

    public ProductsModule()
    {
        Get["/products/{id}"] = DoSomething;
    }
}

来自 Nancy 文档:

A route Action is the behavior which is invoked when a request is matched to a route. It is represented by a lambda expression of type Func<dynamic, dynamic> where the dynamic input is a DynamicDictionary, a special dynamic type that is defined in Nancy and is covered in Taking a look at the DynamicDictionary.

因此,您可以简单地执行以下操作:

Get["/products/{id}"] = DoSomething;

其中 DoSomething 定义为:

private dynamic DoSomething(dynamic parameters)