I am struggling to understand the difference between using individual methods and optional parameters for accepting Get
methods with query parameters for an ApiController
.
I wrote my code in the browser so forgive any errors. I also left out other basic actions for brevity.
Here are my options for the given urls (I am planning on having many more query strings that can be optionally passed in):
GET http://myAppi.com/resources?color=red
GET http://myAppi.com/resources?color=red&shape=round
For my endpoints do I do the following:
[Route("")]
public IEnumerable<string> Get(string color)
{
List<string> someList = new List<string>();
//some selecting linq color logic here
return someList;
}
[Route("")]
Public IEnumerable<string> Get(string color, string shape)
{
List<string> someList = new List<string>();
//some selecting linq color and shape logic here
return someList;
}
Or do I use optional parameters...
[Route("")]
public IEnumerable<string> Get(string color, string shape = null)
{
List<string> someList = new List<string>();
if (String.IsNullOrWhiteSpace(shape))
{
//some selecting linq color logic here
}
else
{
//some selecting linq color and shape logic here
}
return someList;
}
What's the difference?