1

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?

2
  • As the exception message says, Microsoft.AspNetCore.Http.StatusCodes.Status400BadRequest is a const int, not a function. You can't thus invoke it with ( and ). Commented Sep 30, 2019 at 19:34
  • Correct. but is there anyway I can return HTTP codes with custom messages outside of a controller? Commented Sep 30, 2019 at 19:37

1 Answer 1

2

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");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.