0

Let's say that I have

[HttpPost("some/route")]
public void AddSomething([FromBody] QueryParameter parameter){
    DoSomething();
}

and my parameter is:

public class QueryParameter  {
   public int ElementId { get; set; }
   public IEnumerable<int> YearIds{ get; set; }       
}

The question is: can I keep IEnumerable<int> or it should be List<int>? What is better practice and why?

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
DiPix
  • 5,755
  • 15
  • 61
  • 108
  • 1
    For starters, an endpoint should never return `void`, but rather `IActionResult` / `Task` if asynchronous. – Camilo Terevinto Nov 09 '18 at 14:38
  • Then, as always, try for yourself instead of asking someone else whether your own code works or doesn't. – Camilo Terevinto Nov 09 '18 at 14:39
  • It works in both cases... That's why I'm asking. By the way why I have to return value always? I'm using .net core 2.1 and Angular 6 if it matters. – DiPix Nov 09 '18 at 14:41
  • Because you need to be able to be explicit about the result, independent of the actual work done and the result of such operation. – Camilo Terevinto Nov 09 '18 at 14:49
  • Is there is any particular reason why should I do this. If I not expecting any error here? What's the diffrence between `void` and `IActionResult return OK()` both going to return 200. – DiPix Nov 09 '18 at 14:53
  • As I said, it's always best to show that you intended to return 200. Further, if you are not returning any result, you should return 204 -- no content. – Camilo Terevinto Nov 09 '18 at 14:58

0 Answers0