0

How do I pass a null value to an ASP.NET web service when accessing it via HTTP POST?

[WebMethod]
public string GetResult(DateTime date, int foo)
{
  //do stuff here
}

When calling this method using HTTP POST, how do I pass a null parameter? Passing empty string just makes the server throw an exception trying to convert the string parameter to a DateTime. Making the DateTime parameter nullable (i.e. DateTime? date) seems to disallow calling it via HTTP.

1
  • SOAP uses HTTP, usually. You may mean that it disallows calling it via raw HTTP. Commented Feb 19, 2011 at 1:05

1 Answer 1

2

Neither of these can have null values. They are both value types and can't be set to null even if you attempted to do it using code. I would recommend sending DateTime.MinValue and 0.

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

5 Comments

Or int.MinValue instead of 0.
@slugster - Yes another good choice. I've seen MaxValue used as well. I recommended 0 only because it's the default for an un-initialized int, but your suggestion is also useful.
I know, but they can be nullable. So if I make the parameters nullable, can I no longer call it via raw HTTP POST?
@Tom Hamming - no they can't. By their signatures you have DateTime and int. That is not the same as DateTime? and int? (which are nullable). They are not interchangeable in any way. According to the method signature you MUST send a datetime and an int.
That's what I mean. If they're changed to be DateTime? and int? then it disallows calling the service via raw HTTP; I was hoping that doing that would allow me to send null values somehow. In any case, I've gotten around the problem by sending a long-ago date, and then checking for that date server-side. I'd use DateTime.MinValue but I'm not calling the service from .NET.

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.