c# - 没有 lambda 表达式的路由

标签 c# lambda nancy

下面是the official example用于在 Nancy 中注册路线.但是,如果我不想在该方法中“做某事”,而是在 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)

关于c# - 没有 lambda 表达式的路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41502366/

相关文章:

c# - Linq Where条件匹配数组

c++ - 是什么导致 std::bad_function_call?

unit-testing - 试驾南希模块

c# - 使用 Twitterizer 更新 Twitter 状态时出错

C# 控制台应用程序 - OO 数学/思维问题

c# - *创建* Salesforce 对象时出现 "operation performed with inactive user"错误的原因是什么?

c# - .NET 中的正则表达式是如何实现的?

java - 将对象提供给消费者并返回对象

c# - 自托管在 Mono 下运行的简单 NancyFX HelloWorld 应用程序的延迟问题

c# - 让 CORS 与 Nancy 一起工作