Arya
Arya

Reputation: 8995

Using HTTP status codes with custom messages outside of controller

I'm trying to return HTTP codes that would contain a custom message outside of a controller.

For example, in a controller I can have

return BadRequest("Invalid");

Inside a regular method I tried the following code

return Microsoft.AspNetCore.Http.StatusCodes.Status400BadRequest("Invalid");

But I get the following message

"Non-invocable 'StatusCodes.Status400BadRequest' cannot be used like a method"

Is there anyway I can return HTTP codes with custom messages outside of a controller?

Upvotes: 1

Views: 651

Answers (1)

pfx
pfx

Reputation: 23369

BadRequest("Invalid") returns an BadRequestObjectResult instance.

In similar way, you can return an instance of this class from your code via

return new BadRequestObjectResult("Invalid");

Upvotes: 2

Related Questions