Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 4105

Is it possible to allow URL parameters to be passed in like switches?

$
0
0

I'm wondering if it's possible to implement a REST API endpoint in ASP.NET where a URL parameter can be passed in like a switch (as opposed to having to pass in a boolean parameter that has to be explicitly set in the URL to true or false).

I have this controller:

[Route("[controller]")]public class TestController : Controller {    [HttpGet("example")]    public IActionResult Example([FromQuery] bool? myswitch) {        return Ok(new {            ParameterValue = myswitch ?? false,            Message = myswitch.HasValue && myswitch.Value                 ? "Parameter is present"                 : "Parameter is absent/false"        });    }}

If I pass in http://localhost:5005/test/example?myswitch I want to receive:

{"parameterValue": true,"message": "Parameter is present"}

But instead, I'm receiving:

{"parameterValue": false,"message": "Parameter is absent/false"}

Is there a different data type than boolean I could use for this purpose?


Viewing all articles
Browse latest Browse all 4105


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>