c# - NancyFx 将模型绑定(bind)到动态类型?

标签 c# .net model-binding nancy

在 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/

相关文章:

c# - 如何将几个 .NET dll 打包成一个 dll

c# - 一般异常处理问题

c# - ASP.NET Core 发布数组对象 JSON

c# - .Net 核心模型绑定(bind) JSON 发布到 Web API

c# - 如何将dataGridView的前3列readOnly设置为true

c# - 使用 c# 的 asp.net guid 问题

c# - session 超时

c# - WinForms - 如何获得控件 "wants"的大小?

c# - 批量保存到 MongoDB C# 驱动程序

c# - asp.net-mvc(2) 中 Modelbinding double 的 CultureInfo 问题