Dynamic Nancy中的强制转换路由参数始终为空

Dynamic Nancy中的强制转换路由参数始终为空,dynamic,nancy,Dynamic,Nancy,我有一个Nancy模块,它使用一个函数作为参数,该函数需要一个字符串(从路由捕获的模式)和一个方法组。当试图直接传递参数时,它将不会编译,因为我“无法将方法组用作动态调度操作的参数” 我已经创建了第二个路由,它尝试将动态转换为字符串,但它总是返回null using System; using Nancy; public class MyModule : NancyModule { public MyModule() { //Get["/path/{Name}/

我有一个Nancy模块,它使用一个函数作为参数,该函数需要一个字符串(从路由捕获的模式)和一个方法组。当试图直接传递参数时,它将不会编译,因为我“无法将方法组用作动态调度操作的参数”

我已经创建了第二个路由,它尝试将动态转换为字符串,但它总是返回null

using System;
using Nancy;

public class MyModule : NancyModule
{
    public MyModule()
    {
        //Get["/path/{Name}/action"] = parameters =>
        //    {
        //        return MyMethod(parameters.Name, methodToBeCalled); // this does not compile              
        //    };

        Get["/path/{Name}/anotherAction"] = parameters =>
            {
                return MyMethod(parameters.Name as string, anotherMethodToBeCalled);
            };
    }

    public Response MyMethod(string name, Func<int> doSomething)
    {
        doSomething();
        return Response.AsText(string.Format("Hello {0}", name));
    }

    public int methodToBeCalled()
    {
        return -1;
    }

    public int anotherMethodToBeCalled()
    {
        return 1;
    }
}
使用系统;
使用Nancy;
公共类MyModule:NancyModule
{
公共MyModule()
{
//获取[“/path/{Name}/action”]=参数=>
//    {
//返回MyMethod(parameters.Name,methodToBeCalled);//这不会编译
//    };
获取[“/path/{Name}/anotherAction”]=参数=>
{
返回MyMethod(parameters.Name为字符串,anotherMethodToBeCalled);
};
}
公共响应MyMethod(字符串名称,Func doSomething)
{
doSomething();
返回Response.AsText(string.Format(“Hello{0}”,name));
}
public int methodToBeCalled()
{
返回-1;
}
public int-anotherMethodToBeCalled()
{
返回1;
}
}
在单独的项目中使用以下类别进行测试:

using System;

using Nancy;
using Nancy.Testing;
using NUnit.Framework;

[TestFixture]
public class MyModuleTest
{
    Browser browser;

    [SetUp]
    public void SetUp()
    {
        browser = new Browser(with =>
        {
            with.Module<MyModule>();
            with.EnableAutoRegistration();
        });
    }

    [Test]
    public void Can_Get_View()
    {
        // When
        var result = browser.Get("/path/foobar/anotherAction", with => with.HttpRequest());

        // Then
        Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
        Assert.AreEqual("Hello foobar", result.Body.AsString()); //fails as parameters.Name is always null when cast to a string
    }
}
使用系统;
使用Nancy;
使用Nancy测试;
使用NUnit.Framework;
[测试夹具]
公共类MyModuleTest
{
浏览器;
[设置]
公共作废设置()
{
浏览器=新浏览器(使用=>
{
带.Module();
with.EnableAutoRegistration();
});
}
[测试]
public void可以获取视图()
{
//什么时候
var result=browser.Get(“/path/foobar/anotherAction”,with=>with.HttpRequest());
//然后
AreEqual(HttpStatusCode.OK,result.StatusCode);
Assert.AreEqual(“Hello foobar”,result.Body.AsString());//作为参数失败。转换为字符串时,名称始终为null
}
}

您可以在

上找到整个测试,我在使用“as”时遇到了类似的问题,因此我倾向于使用显式强制转换:

return MyMethod((string)parameters.Name, anotherMethodToBeCalled);
另外,我认为参数的大小写会引起一个bug,但我认为最好保持小写:

Get["/path/{name}/anotherAction"]

(string)parameters.name

您的代码可以使用大写和小写,并使用显式转换

嗯,这真是太简单了!:)非常感谢。