1

I build my first mvc6 api with :

https://docs.asp.net/en/latest/tutorials/first-web-api.html

I wanted to write test for it now but how can I cast a IActionResult which returns a JSON string to an Model(e.g User)

I get a error which says:

Cannot Convert IAction result to User ...

[HttpGet("{}")]
public IActionResult getUser()
{
  Benutzer benutzerData = _context.benutzer.FirstOrDefault<User>();

  return new ObjectResult(benutzerData);
}

and in my Test class :

  Benutzer user = _controller.getUser();
1
  • Ok i found a way with (ObjectResult) cast ... Mar 4, 2016 at 12:55

1 Answer 1

3

You can try this

[HttpGet("{}")]
public IActionResult getUser()
{
  Benutzer benutzerData = _context.benutzer.FirstOrDefault<User>();

  return Ok(benutzerData);
}

you cannot store json string to model class object instead you can store it in string and you can pass the string to the model.And also use cannot call controller in model class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.