0

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.

7
  • Did you try to omit fingerprint parameter? I mean, just don't send the parameter at all. Commented Nov 6, 2019 at 21:35
  • You could try setting a default value of null for your fingerprint argument and handle the null check within your method. Commented Nov 6, 2019 at 21:35
  • but i need to send the property_id along Commented Nov 6, 2019 at 21:35
  • Not sure this helps.. but JS represents Guids with the string type which is not nullable. So you want to type it as a string | null. Tldr, can you try sending from Postman? Commented Nov 6, 2019 at 21:37
  • Parameters with default values need to be on the end of your argument list. It won't change the behavior of your other arguments. Commented Nov 6, 2019 at 21:37

1 Answer 1

3

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.

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

Comments

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.