Looks like binder for date works incorrect in asp.net mvc

后端 未结 1 1739
忘掉有多难
忘掉有多难 2021-01-06 05:22

\"enter

I have startdate in QueryString with value: 3/1/2012

DateTime.Parse(Re

相关标签:
1条回答
  • 2021-01-06 06:10

    The default model binder always uses InvarianCulture when parsing query string values, no matter which culture you configured in your web.config.

    • GET => InvariantCulture
    • POST => culture agnostic

    So assuming you have the 2 actions:

    [HttpGet]
    public ActionResult Foo(DateTime date)
    {
        ...
    }
    
    [HttpPost]
    public ActionResult Bar(DateTime date)
    {
        ...
    }
    

    when you invoke the Foo action you should always use the invariant culture to format the date in the query string, whereas when you invoke the Bar action and pass the date parameter in the POST body payload, the default model binder will use the culture configured in your web.config.

    Take a look at the following blog post which covers this in more details.

    0 讨论(0)
提交回复
热议问题