Optional parameters: empty and not provided

Is there a way to distinguish between an empty value and a value that is not mentioned at all?

For example, I would like the client to be able to update the profile and influence only the values ​​specified in the request ... EVEN empty / null.

therefore, a profile with name = "Sherlok" and Birthdate = "January 6" is given:

POST api/profiles/update?name=Sherlock >> only modifies name

POST api/profiles/update?birthdate=&name=Sherlock >> modifies name AND clears birthdate

Is there a way to differentiate the parent date parameter in these two situations, or do I need to resort to magic values?

+3
source share
2 answers

If you use ASP.NET web API binding, this is not possible. Say you have a DTO class and use it to bind like this.

public class MyDto
{
    public string Name { get; set; }
    public string BirthDate { get; set; }
}

public void Update([FromUri]MyDto dto) { }

BirthDate , , BirthDate null . .

string qs = Request.RequestUri.Query.Substring(1); // leave out ?
var pairs = System.Web.HttpUtility.ParseQueryString(qs);

pairs["BirthDate"] null, (? name = Sherlock).

(? birthdate = & name = Sherlock), pairs["BirthDate"] ("").

0

.

  • @Badri, Request . , /. , .
  • , . , . Request - @Badri (String.Empty null), .
  • . , DateTime. , (, IntuitiveDateTimeValueProvider), DateTime Nullable<DateTime> (DateTime?), . , (, ExistenceTestingStringValueProvider).
  • /. , , , , . - . , name, , birthdate , , , birthdate, null, , .
0

All Articles