❔ WEBAPI [FromBody] Json with optional elements
I am building a .net Wep API application where I will be receiving requests where all of the data is in the JSON body. I understand this is not the preferred way to do this but this is how I have to do it for this application. I will receive a post from my frontend which essentially looks like this
My code is able to process this provided both startTimestamp and endTimestamp are occupied. However as they are optional attributes they will sometimes be absent from requests and in those cases I receive an error
I would like to be able to make these field not required but I am uncertain how.
My method which receives the request starts like this
15 Replies
and a MessageUpdate looks like this
Make the optional properties nullable?
through like an attribute tag do you mean?
Through... making them nullable
aren't strings nullable?
What version of .NET?
7
Then no, they're not
Unless you manually edited the project file to make reference types nullable by default
interesting. How can I change what I have to make it nullable then?
string?
Like with literally any other type
T
is not nullable, T?
is nullableso like
not sure why it spaced that all wierdly
That should do the trick, yeah
Awesome! Let me try it out
Heyyyy! that worked! Thank you so much! My friend is hounding me relentlessly to solve this part of my code and its stressing me out. You're a life saver
Nice
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.