ASP.NET Core and CreateErrorResponse

Viewed 9195

I want to return invalid ModelState from my API method and try to use as it was in Web Api:

            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Test");

but it says, that

Severity Code Description Project File Line Suppression State Error CS1929 'HttpRequest' does not contain a definition for 'CreateErrorResponse' and the best extension method overload 'HttpRequestMessageExtensions.CreateErrorResponse(HttpRequestMessage, HttpStatusCode, string)' requires a receiver of type 'HttpRequestMessage'

So, something changed in ASP.NET Core and I should include HttpRequestMessage as first parameter. But how to create it and why it's necessary in response at all?

EDIT:

I found, that invalid model can be returned by the following code:

return HttpBadRequest(ModelState);

but in any case, want to know flexible method to return my own error responses.

1 Answers
Related