Yehia A.Salam
Yehia A.Salam

Reputation: 2078

Send nullable Guid to WebApi Asp.net

I have a web api end point with the declaration:

public async Task VisitorConnected(Guid? fingerprint, long property_id)

Sometimes I want to send null for the first parameter. I read on one of the threads to send it as "null" in a string quotation. However I'm getting:

System.IO.InvalidDataException: Error binding arguments. Make sure that the types of the provided values match the types of the hub method being invoked.
 ---> System.FormatException: The JSON value is not in a supported Guid format.
   at System.Text.Json.Utf8JsonReader.GetGuid()
   at System.Text.Json.Serialization.Converters.JsonConverterGuid.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options)
   at System.Text.Json.JsonPropertyInfoNullable`2.OnRead(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader)
   at System.Text.Json.JsonPropertyInfo.Read(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader)

I tried to look it up in https://github.com/dotnet/corefx/blob/master/src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.TryGet.cs#L940 but it appears that the Json Parser is not handling this null case. How can i achieve this, my second best option is to send an empty Guid 000-00000000-00-000... from the javascript client, and check in the web api controller, but feels like a hack.

Upvotes: 0

Views: 2352

Answers (1)

Cameron Tinker
Cameron Tinker

Reputation: 9789

Try changing your Web API method to the following:
public async Task VisitorConnected(long property_id, Guid? fingerprint = null)

This will allow setting fingerprint to null while leaving property_id as a required argument to your method.

Upvotes: 3

Related Questions