Search code examples
c#asp.net-coreasp.net-core-mvcasp.net-core-3.1

Difference between BadRequestResult and BadRequestObjectResult


I'm going to return a bad request result in my action filter.

I have two choices:

context.result = new BadRequestResult();

or

context.result = new BadRequestObjectResult();

It seems there are couple types for all status codes: NotFoundResult - NotFoundObjectResult and so on.

which one I should use and what is differences of these couple types?


Solution

  • Both are for similar purpose, but the second one (BadRequestObjectResult) with the difference, an object or a ModelStateDictionary can be passed as a constructor argument, containing the details regarding the error.

    Take a look at the constructors in below link.

    https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.badrequestobjectresult?view=aspnetcore-3.1