在 Nancy 中,有没有办法将 POST 请求的内容绑定(bind)到动态类型?
例如:.
// 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>();
}
最佳答案
开箱即用的 Nancy 不支持动态模型绑定(bind)。 TheCodeJunkie 已经编写了一个快速的 ModelBinder 来实现这一目标。
https://gist.github.com/thecodejunkie/5521941
然后就可以这样使用了
dynamic model = this.Bind<DynamicDictionary>();
关于c# - NancyFx 将模型绑定(bind)到动态类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16722311/