简体   繁体   中英

ASP.NET Core API not accepting null value for a nullable type

I have a GET request API endpoint that receives a request View Model with a Nullable property like this:

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
    }

but when I make a get request for MakeId with a null value, The API is not accepting null value for this nullable type.

this is my get request: https://localhost:44311/api/model/getlist?MakeId=null

I get the following error:

The value 'null' is not valid for MakeId.

MakeId is a Nullable<uint> but MakeId=null in the query string null is just a string value which can not be parsed as uint. just omit it from your request to make it null:

https://localhost:44311/api/model/getlist

You can do something like this to include makeid if it's not null:

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

It is actually receiving the string "null" and can not convert it to uint .

The value would be null if you omit it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM