【发布时间】:2020-03-03 10:43:16
【问题描述】:
我有一个带有声明的 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 控制器,但感觉就像一个黑客。
【问题讨论】:
-
您是否尝试省略
fingerprint参数?我的意思是,根本不发送参数。 -
您可以尝试为您的
fingerprint参数设置默认值null并在您的方法中处理 null 检查。 -
但我需要同时发送 property_id
-
不确定这是否有帮助.. 但 JS 表示具有不可为空的
string类型的 Guid。所以你想把它输入为string | null。 Tldr,您可以尝试从 Postman 发送吗? -
具有默认值的参数需要位于参数列表的末尾。它不会改变你其他论点的行为。
标签: c# asp.net asp.net-core asp.net-web-api