简体   繁体   English

Asp.Net WebApi模型绑定可为空的日期时间

[英]Asp.Net WebApi model binding nullable datetime

Hello, 你好,

I have api GET-method /rating (ASP.Net WebApi 2.1), which accepts objects of type ChartPageRequest : 我有api GET-method /rating (ASP.Net WebApi 2.1),它接受ChartPageRequest类型的ChartPageRequest

// comments're removed for readability
public sealed class ChartPageRequest
{
    public DateTime? From { get; set; }

    public DateTime? To { get; set; }

    public string Cursor { get; set; }

    [Range(-100, 100)]
    public int Take { get; set; } = 10;
}

/rating method has following signature: /rating方法具有以下签名:

[HttpGet]
[Route("rating")]
[ResponseType(typeof(ChartPage))]
[ValidateModelState]
public async Task<IHttpActionResult> GetTranslationRatingChartAsync([ModelBinder] ChartPageRequest model)
{
    // body here
}

And ValidateModelState attribute is just a custom attribute which returns custom response when ModelState isn't valid. ValidateModelState属性只是一个自定义属性,当ModelState无效时,它将返回自定义响应。 It doesn't check anything by itself except HttpActionContext.ModelState.IsValid property. 除了HttpActionContext.ModelState.IsValid属性外,它本身不会检查任何内容。

This api method works fine except one case - when client explicitly passes null value to DateTime? 除一种情况外,此api方法工作正常-当客户端明确将null值传递给DateTime? properties of ChartPageRequest , eg: ChartPageRequest属性,例如:

/rating?from=2016-07-08 12:01:55.604&to=null

In this case ValidateModelState attribute registers invalid ModelState with following message: The value 'null' is not valid for To . 在这种情况下,ValidateModelState属性使用以下消息注册无效的ModelState: The value 'null' is not valid for To

I've found this kind of problem quite popular, but haven't found any good workarounds without creating custom model binder. 我发现这种问题非常流行,但是如果不创建自定义模型绑定程序,就找不到任何好的解决方法。 So here's the questions: 所以这是问题:

  1. Is there another approach without custom model binder? 是否有没有自定义模型绑定器的另一种方法?

  2. In case there isn't, how can I take only "accepting null" job and leave DateTime parsing to default binder in my custom binder? 如果没有,我如何只接受“接受null”作业,而将DateTime解析留给我的自定义资料夹中的默认资料夹?

  3. Do I need to accept those nulls at all? 我是否需要接受所有这些空值? All clients are developing by my colleagues so I can force them to not send it. 所有客户都是由我的同事开发的,因此我可以强迫他们不要发送。 Is it good practice after all? 毕竟是好习惯吗?

Thanks! 谢谢!

I didn't validate this method, but I guess you can try parse it yourself, though I think it's not a convenient way, first use a string to get the To : 我没有验证此方法,但是我想您可以尝试自己解析它,尽管我认为这不是一种方便的方法,请首先使用字符串获取To

public string To { get; set; }

And give another property DateTimeTo and parse To yourself: 并给另一个属性DateTimeTo并解析To自己:

public DateTime? DateTimeTo { get; set; }

public void ParseTo()
{
    if(To.ToLower() == "null")
        DateTimeTo = null;
    else
        DateTimeTo = Convert.ToDateTime(To); 
}

And DateTimeTo is the parsed result property. DateTimeTo是已解析的结果属性。

I recently ran into some Asp.Net WebApi parsing body issue, the parsing doesn't work very well in some condition. 我最近遇到了一个Asp.Net WebApi解析主体问题,在某些情况下解析无法很好地进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM