ASP.NET Core API不接受可为空类型的空值

Zee*_*dil 4 c# asp.net-core asp.net-core-webapi

我有一个GET请求API端点,该端点接收具有Nullable属性的请求视图模型,如下所示:

public class ModelRequestVM : BaseRequestVM
    {
        #region Properties and Data Members
        public uint ModelId { get; set; }
        public uint? MakeId { get; set; }
        public string MakeName { get; set; }
        public string ModelName { get; set; }
        public uint? ExternalModelId { get; set; }

        #endregion
    }
Run Code Online (Sandbox Code Playgroud)

但是当我请求MakeId一个带有null值的get请求时,API不接受null此可空类型的值。

这是我的要求: https://localhost:44311/api/model/getlist?MakeId=null

我收到以下错误:

The value 'null' is not valid for MakeId.

Ash*_*ani 7

MakeId是一个,Nullable<uint>但是MakeId=null查询字符串中的字符串null只是一个不能解析为uint的字符串值。只需从您的请求中将其忽略即可将其设为null:

https://localhost:44311/api/model/getlist
Run Code Online (Sandbox Code Playgroud)

如果不为null,则可以执行以下操作以包含makeid:

string url = $"https://localhost:44311/api/model/getlist(makeid == null?"":"?makeid=" + makeid)";
Run Code Online (Sandbox Code Playgroud)


Ale*_*zić 5

它实际上正在接收字符串“null”并且无法将其转换为uint.

如果省略它,该值将为 null。