有什么方法可以检测Nancy请求正文中是否存在可为空的参数

时间:2019-05-07 15:48:51

标签: nancy

背景

我有一个使用Nancy 1.4.3的Web服务器。 Nancy模型绑定用于从具有JSON正文的HTTP请求中反序列化数据。参见下面的示例。

示例

服务器中的端点:

private class ARequestBody
{
    public int? Parameter1 { get; set; }
    public int? Parameter2 { get; set; }
}

[Post("examplerequest")]
public Response AMethod()
{
    var body = this.Bind<ARequestBody>

    //Do stuff with data in body
    var foo = body.Parameter1;
    var bar = body.Parameter2;

    return new Response();
}

请求终结点:

Method: POST
Path: [server_address_etc]/examplerequest
Body:
{
    "Parameter1" : 123,
    "Parameter2" : null
}

如果对此端点发出请求,例如请求主体中的Parameter1为null,则绑定后body.Parameter1的值为null是可以理解的。但是,如果请求中未包含Parameter1,则body.Parameter1也将为null。

问题

有什么方法可以检测请求主体中是否包含参数?如果可能的话,我希望能够辨别:

  1. 可为空的参数包含在请求正文中,但值为null
  2. 如果参数根本不包含在请求正文中。

0 个答案:

没有答案
相关问题