'Can a controller ever accept a null parameter value?

If I had a controller method like so:

public ActionResult<Item> GetItem(RequestHeaderBase headers, RequestObject request)

Can I always assume that the headers object and request object will be instantiated (not null)?

(I would appreciate any links to deeper reading regarding how this works regardless of the answer)

Here is an example of what I mean

Here I call the request: https://localhost:44360/weatherforecast and you can see the input object has been instantiated despite me not providing the query string. enter image description here

Another example, this time with data from the body: enter image description here enter image description here

In both cases, you can see with my debugging, no matter what the objects are instantiated, even if I don't provide any data in the request.

Now I've seen code that checks if this data is null in some of our applications, I believe this code is irrelevant, and so can be removed.



Solution 1:[1]

Can I always assume that the headers object and request object will be instantiated (not null)?

no, they can be null.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kerollos Melad