c# - 将可为空的 Guid 发送到 WebApi Asp.net
问题描述
我有一个带有声明的 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
其作为方法的必需参数。
推荐阅读
- android-studio - 在 android studio 中为特定风格设置 buildType 属性
- ios - 斯威夫特:代表为零
- multithreading - Docker 容器而不是多处理
- r - 如何使用 RevoScaleR 在 SQL Server 中创建表?
- swift - 无法将“AProtocol”.Protocol 类型的值转换为预期的参数类型协议
- javascript - 如何计算 window.prompt 中的单词并按出现次数对其进行排序?
- wordpress - Wordpress自定义字段在指定日期后删除帖子?
- android - 如何使用 window.setSoftInputMode() 组合状态和调整
- ajax - Django 文件上传 Ajax
- javascript - 如何使用 Webpack 独立捆绑 JS 和 CSS 文件?