C# 示例应用程序中的Lambda错误
在尝试让简单的Nancy示例程序工作时,我收到了一个错误,这让我有些困惑C# 示例应用程序中的Lambda错误,c#,rest,lambda,compiler-errors,nancy,C#,Rest,Lambda,Compiler Errors,Nancy,在尝试让简单的Nancy示例程序工作时,我收到了一个错误,这让我有些困惑 namespace NancyServer { using Nancy; public class ServerApi : NancyModule { public ServerApi() { Get["/"] = _ => "Hello World"; } } } Visual studio 2013 pro在我的路线中阻碍了l
namespace NancyServer {
using Nancy;
public class ServerApi : NancyModule {
public ServerApi() {
Get["/"] = _ => "Hello World";
}
}
}
Visual studio 2013 pro在我的路线中阻碍了lambda
_ => "Hello World";
我还尝试了其他一些表达,它们都有相同的问题
Get["/products/{id}"] = _ => {
System.Console.WriteLine( "test" );
};
上面的代码片段也是直接从Nancy wiki中提取的
Intellisense在lambda赋值\u=>
下加下划线,表示委托不接受一个参数。如果我尝试构建,会出现三个错误
Error 1 Delegate 'System.Func<dynamic,System.Threading.CancellationToken,System.Threading.Tasks.Task<dynamic>>' does not take 1 arguments
Error 2 Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<dynamic>'
Error 3 Cannot convert lambda expression to delegate type 'System.Func<dynamic,System.Threading.CancellationToken,System.Threading.Tasks.Task<dynamic>>' because some of the return types in the block are not implicitly convertible to the delegate return type
我通过VS2013 pro中的Nuget直接添加了Nancy和Nancy.Hosting.Self。更改如下:
Get[“/”]=(参数,令牌)=>Task.FromResult(“Hello World”)代码>
Nancy v2.0.0现在一直是异步的。将其更改为:
Get[“/”]=(参数,令牌)=>Task.FromResult(“Hello World”)代码>
南茜V2.0.0现在是异步的。
只是澄清,第二个参数是<代码>取消标记[/Cord]。南茜2.0.0是预发布alpha,您可能需要考虑在学习时使用最新的稳定版本。在你学习的时候,你可能想考虑使用最新的稳定版本。
namespace NancyServer {
using System;
using Nancy.Hosting.Self;
public class ServerMain {
static void Main( string[] args ) {
var nancyHost = new NancyHost( new Uri( "http://localhost:25000" ) );
nancyHost.Start();
Console.WriteLine( "Server running..." );
Console.ReadKey();
nancyHost.Stop();
}
}
}