`CreatedResult` should accept null location
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Describe the bug
RFC7231 suggests that the Location
header is optional for 201
Created response.
The primary resource created by the request is identified by either a Location header field in the response or, if no Location field is received, by the effective request URI.
However, currently the constructors of CreateResult
throws if null
is passed to location
, and ControllerBase.Created()
throws if null
is passed to url
.
Expected Behavior
A response of 201
Created without Location
header is created.
Steps To Reproduce
[HttpPost]
public ActionResult Create()
{
return new CreatedResult((string?)null, null); // throws ArgumentNullException
}
[HttpPost]
public ActionResult Create()
{
return Created((string?)null, null) // throws ArgumentNullException
}
Exceptions (if any)
System.ArgumentNullException: Value cannot be null. (Parameter 'location')
at Microsoft.AspNetCore.Mvc.CreatedResult..ctor(String location, Object value)
...
.NET Version
6.0.101
Anything else?
No response
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:28 (28 by maintainers)
Top Results From Across the Web
IHttpActionResult variable passing null in test method
I am writing a test method for an IHttpActionresult controller. The ActionResult is not NULL and contains the required data (Customer.ID = ...
Read more >Action Result in ASP.NET Core API
An AcceptedAtRouteResult returns an Accepted (202) response with a Location header. It's the same as AcceptedResult, with the only difference ...
Read more >Controller action return types in ASP.NET Core web API
A Location response header containing the newly created product's URL is provided. For example, the following model indicates that requests must ...
Read more >Asp.Net Core Action Results Explained - Hamid Mosalla
CreatedResult returns 201 status code along with a URI to the created resource. You should use it when you creating a resource, and...
Read more >Unit Testing Controllers in ASP.NET Web API 2
Here are some things that you should unit test in your Web API ... Otherwise, the test will fail with an ArgumentNullException or ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It’s too bad that
uri
is beforevalue
already, but I don’t think omitting a preceding parameter in one (or some) overloads is as bad as changing order between overloads (with same or more parameters).The fact that there’s two overloads makes it even more compelling to add a single-parameter overload, since you need those ugly
(string?)null
casts everywhere to avoid ambiguity 😞The approved API does not remove any constructors or methods. It merely changes the nullability annotations of some parameters.
If you need to remove test cases for them to compile or pass, that’s an indication of a breaking change which we will not accept.
It’s important that developers can confidently upgrade between versions of ASP.NET Core without updating a bunch of code. This sometimes means saying no to cleaning up APIs that are good enough because doing so would break already working applications.
I recommend using markdown for code snippets so we can search the content and more easily quote it.
This can only work because of the breaking change. This won’t compile with the approved changes. You’ll get the same error demonstrated by the sharplab.io sample I posted in my last comment on this issue. The following will compile though without warnings and work.
Currently, this would give nullability warnings and throw an
ArgumentNullException
at runtime.