我有一个带有声明的Web api端点:
public async Task VisitorConnected(Guid? fingerprint, long property_id)
有时我想发送null
作为第一个参数。我读了其中一个线程,以字符串引用的形式将其作为"null"
发送。但是我得到:
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)
我试图在https://github.com/dotnet/corefx/blob/master/src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.TryGet.cs#L940中查找它,但是Json Parser似乎没有处理这种null
情况。我怎么能做到这一点,我的第二个最佳选择是从JavaScript客户端发送一个空的Guid 000-00000000-00-000 ...,并检入Web api控制器,但感觉像是被黑客入侵了。
尝试将您的Web API方法更改为以下内容:public async Task VisitorConnected(long property_id, Guid? fingerprint = null)
这将允许将fingerprint
设置为null,同时将property_id
保留为方法的必需参数。