Reputation: 35
When I used MVC controllers I used "return OK(object);" or "return BadRequest(ErrorMessage)" and such.
How can I achieve this is Razor Pages?
I tried return new JsonResult(object); which works when the status code can be 200. But what if I want to return status code 400 with a JSON error message.
Upvotes: 2
Views: 4376
Reputation: 30035
You can return a JsonResult
from a Razor Page handler method, and set the HTTP status code for the response:
public IActionResult OnGet()
{
Response.StatusCode = 400;
return new JsonResult(new { message = "Error" } );
}
Upvotes: 4