gpt4 book ai didi

.net - 南希FX : Deserialize JSON

转载 作者:行者123 更新时间:2023-12-04 00:24:11 25 4
gpt4 key购买 nike

文档建议 NancyFx 帮助我解决 json 请求正文的 WRT 反序列化,但我不确定如何。请参阅下面的测试以演示:

[TestFixture]
public class ScratchNancy
{
[Test]
public void RootTest()
{
var result = new Browser(new DefaultNancyBootstrapper()).Post(
"/",
with =>
{
with.HttpRequest();
with.JsonBody(JsonConvert.SerializeObject(new DTO {Name = "Dto", Value = 9}));
});

Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
}

public class RootModule : NancyModule
{
public RootModule()
{
Post["/"] = Root;
}

private Response Root(dynamic o)
{
DTO dto = null;//how do I get the dto from the body of the request without reading the stream and deserializing myself?

return HttpStatusCode.OK;
}
}

public class DTO
{
public string Name { get; set; }
public int Value { get; set; }
}
}

最佳答案

Model-binding

var f = this.Bind<Foo>();

编辑(为了这个问题的其他读者的利益,将上面放在上下文中)
public class RootModule : NancyModule
{
public RootModule()
{
Post["/"] = Root;
}

private Response Root(dynamic o)
{
DTO dto = this.Bind<DTO>(); //Bind is an extension method defined in Nancy.ModelBinding

return HttpStatusCode.OK;
}
}

关于.net - 南希FX : Deserialize JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10723044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com