Reputation: 13
[HttpGet("{id}")]
public Visitor Get(string id)
{
var result = _visitorRepository.GetFromDB(id);
if (result == _visitorRepository.GetFromDB(id))
return StatusCode(200);
if (result != null)
return result;
else
return StatusCode(408);
}
It gives me an error because StatusCode has ActionResult return type
[HttpGet("{id}")]
public ActionResult Get(string id)
{
var result = _visitorRepository.GetFromDB(id);
if (result == _visitorRepository.GetFromDB(id))
return StatusCode(200);
if (result != null)
return result;
else
return StatusCode(408);
}
It gives me an error because result has Visitor return type
I need only one method but I cannot convert the returning type from ActionResult to visitor and vice versa.
Upvotes: 0
Views: 2820
Reputation: 13
Solution:
[HttpGet("{id}")]
public IActionResult Get(string id)
{
if (id == null)
{
return BadRequest();
var result = _visitorRepository.GetFromDB(id);
}
if (result != null)
return Ok(result);
else
return NotFound();
}
Upvotes: 0
Reputation: 1485
You can correct both of your functions as per below explanation.
In case your first function:
[HttpGet("{id}")]
public Visitor Get(string id)
{
var result = _visitorRepository.GetFromDB(id);
if (result != _visitorRepository.GetFromDB(id))
return StatusCode(200); // Somehow make this to return "Visitor" type
if (result != null)
return result; // Somehow make this to return "Visitor" type
else
return StatusCode(408); // Somehow make this to return "Visitor" type
}
If you want to go with the second function then make the following changes:
[HttpGet("{id}")]
public ActionResult Get(string id)
{
var result = _visitorRepository.GetFromDB(id);
if (result != _visitorRepository.GetFromDB(id))
return Ok();
if (result != null)
return Ok(result); // Return type of ActionResult
else
return BadRequest();
}
Just for your reference ActionResult
for StatusCode(200)
return type could be like
return Ok();
Upvotes: 5
Reputation: 541
return new HttpStatusCodeResult(HttpStatusCode.OK); // OK = 200
https://forums.asp.net/t/2084457.aspx?How+do+I+return+HttpStatus+codes+in+ASP+NET+Core+1+0+API+
Upvotes: 0