gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:31:23 26 4
gpt4 key购买 nike

下面是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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com