Let's say I have two endpoints:
[HttpGet ("{serialNumber}")]
and
[HttpGet ("summary")].
As a result, my application perceives api/summary as api/{serialNumber} where {serialNumber} = "summary" and I get the wrong behavior that I want.
CodePudding user response:
the only way to resolve it you have to add action name in the route
[HttpGet ("GetSerialNumber/{serialNumber}")]
public ActionResult GetSerialNumber(string serialNumber)
and
[HttpGet ("GetSummary/{summary}")]
public ActionResult GetSummary(string summary)
CodePudding user response:
I tried it on my machine with different sdks: net3.1, .net5 and .net6
... working as expected.
[HttpGet("values/summary")]
public IActionResult GetSummary()
{
return Ok(new ValueObject[] { new ValueObject { Value = "SUMMARY" } });
}
[HttpGet("values/{serialNumber}")]
public IActionResult GetBySerial([FromRoute] string serialNumber)
{
return Ok(new ValueObject[] { new ValueObject { Value = serialNumber } });
}
If I now call /values/summary
the result is
{
"value": "SUMMARY"
}
else if I call /values/abcde
the result is
{
"value": "abcde"
}
So in my opinion if you never have a {serialNumber} == 'summary'
this APIs should work in the correct way.
If you have possibly the case of a 'summary' == {serialNumber}
use explicit endpoints instead like described before (@Serge).