0

I can't seem to understand what governs how DateTime values are parsed when WebAPI's binding framework operates. Some test code:

public class TestSearchModel
{
    public DateTime? From { get; set; }
    public DateTime? Until { get; set; }
    public string Freetext { get; set; }
}

...

    public HttpResponseMessage Get([FromUri] TestSearchModel searchModel)
    {
        //CultureInfo culture = Thread.CurrentThread.CurrentCulture;

        return Request.CreateResponse(
            $"Received search request with Freetext: {searchModel.Freetext}, From: {searchModel.From}, Until: {searchModel.Until}");
    }

And then I test with various requests like:

http://localhost:9000/api/search?freetext=oi-vei!&from=14.07.2020 (1)

http://localhost:9000/api/search?freetext=oi-vei!&from=07.14.2020 (2)

http://localhost:9000/api/search?freetext=oi-vei!&from=2020-07-14 (3)

Freetext is only there as a test - it always binds just fine. Dates in format like (3) bind fine, I guess that is close enough to culture-invariant format. What I do not get is why the format (1) would not work when the application's culture is set to something with this date format (Norwegian in my case).

Our intention is to have consumers of the API use culture-invariant formats in any case, but we also thought it would be nice to be able to recognize national formats - when we anyway set the current culture from the requests' headers. I realize I can hook up my own binder and program whatever I want, but this seems like something that the framework should already have. It does parse the datetime values one way or another. So the question is: how does the binding system decides what date format to use when attempting parsing a date value?

1
  • Some additional details: the web app is hosted in a console application, in Owin http service. Configuration is very close to default, I just added a couple of message handlers (for setting culture from request and for debug purposes). Neither do anything to the request before passing it to base methods. Commented Jul 15, 2020 at 8:57

1 Answer 1

1

I,m not sure but I hope this code helps you more. You can change your date format and also pass case sensitive parameters in quertString.

http://localhost:9000/api/search?Freetext=oi-veii&From=14-07-2020

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but the question wasn't "how do i make it work". Case-sensitivity has nothing to do with the issue, either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.