4

I'm new to asp.net core 3, sorry if my question sounds too basic, below is my controller class:

[ApiController]
[Route("[controller]")]
public class MyController : ControllerBase {
   List<string> _fruit = new List<string> { "Pear", "Lemon", "Peach" };
  
   [HttpGet("fruit")]
   public IEnumerable<string> MethodXXX() {
      return _fruit;   
   }
   ...
}

so when I route to my/fruit, I get a list of string.

But if I change the MethodXXX to :

[HttpGet("fruit")]
public IEnumerable<string> MethodXXX() {
   return null;
}

Then the browser always fall back to the previous url, e.g I'm on my/other in the beginning, then I change the url to my/fruit, I can see that the browser sends a new request and the url changes to my/fruit for a short period of time then it fall back to my/other again.

What is this behavior for? I am pretty sure I read from a book which says that you can return null from action method?

3
  • may be it is a cache issue. clear the cache and try again Commented Jul 26, 2021 at 7:08
  • Please share the request and response details from Network tab in Chrome for the request. Commented Jul 26, 2021 at 7:09
  • Also, it is very weird to return null for an enumerable. As in "almost certainly wrong". Enumerable.Empty is far more likely to be what you should do. Commented Jul 26, 2021 at 7:10

2 Answers 2

8

This is a behavior newly introduced in .Net Core 3 where if the Action method returns null, it just sends a 204 response which represents No Content. If you desire to return null from an action method, you can override this behavior by using below code block inside Startup.cs file which removes the corressponding Output formatter and you will get null returned from the API response.

services.AddControllers(options =>
        {
            options.OutputFormatters.RemoveType<HttpNoContentOutputFormatter>();
        });
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer. So why the browser fall back to the previous url?why not just stay on the current url which is the receiver of 204 response?
The answer lies here: developer.mozilla.org/en-US/docs/Web/HTTP/Status/204 If you open this the very first line states "The HTTP 204 No Content success status response code indicates that a request has succeeded, but that the client doesn't need to navigate away from its current page." and that's why it doesn't navigate to the other route. Since it doesn't navigate, it looks like it fallback to the same URL as before but it doesn't explicitly send another request to fallback and just remains on the same page as before.
hi can you have a look at this question please stackoverflow.com/questions/68688785/…
-1

you most changed

[HttpGet]
[ActionName("fruit")]
public IEnumerable<string> MethodXXX() {
return null;
}

1 Comment

Welcome to StackOverflow. Please provide some reasoning why do you think that your proposed solution might help the OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.