NancyFx将模型绑定到动态类型?

use*_*502 8 .net c# model-binding nancy

在Nancy中,有没有办法将POST请求的内容绑定到动态类型?

例如:.

// sample POST data: { "Name": "TestName", "Value": "TestValue" }

// model class
public class MyClass {
    public string Name { get; set; }
    public string Value { get; set; }
}

// NancyFx POST url
Post["/apiurl"] = p => {

    // this binding works just fine
    var stronglyTypedModel = this.Bind<MyClass>();

    // the following bindings do not work
    // there are no 'Name' or 'Value' properties on the resulting object
    dynamic dynamicModel1 = this.Bind();
    var dynamicModel2 = this.Bind<dynamic>();
    ExpandoObject dynamicModel3 = this.Bind();
    var dynamicModel4 = this.Bind<ExpandoObject>();

}
Run Code Online (Sandbox Code Playgroud)

Phi*_*ill 9

开箱即用Nancy不支持动态模型绑定.TheCodeJunkie写了一个快速的ModelBinder来实现这个目的.

https://gist.github.com/thecodejunkie/5521941

然后就可以这样使用它

dynamic model = this.Bind<DynamicDictionary>();