将可为空的向导发送到WebApi Asp.net

问题描述 投票:0回答:1

我有一个带有声明的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控制器,但感觉像是被黑客入侵了。

c# asp.net asp.net-core asp.net-web-api
1个回答
0
投票

尝试将您的Web API方法更改为以下内容:public async Task VisitorConnected(long property_id, Guid? fingerprint = null)

这将允许将fingerprint设置为null,同时将property_id保留为方法的必需参数。

© www.soinside.com 2019 - 2024. All rights reserved.