7

ドキュメントは、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; }
    }
}
4

1 に答える 1

15

モデルバインディング

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;
    }
}
于 2012-05-23T15:38:41.433 に答える