Difference between BadRequestResult and BadRequestObjectResult
Asked Answered
M

2

9

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?

Maillot answered 9/7, 2020 at 15:8 Comment(0)
A
11

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

Architect answered 9/7, 2020 at 16:14 Comment(0)
S
0

Apart from additional constructor the only difference is that BadRequestObjectResult in its constructors sets Status code to 400.

var badRequestObjectResult = new BadRequestObjectResult((object?)null);
var objectResult = new ObjectResult(null);
Console.WriteLine(badRequestObjectResult.StatusCode);
Console.WriteLine(objectResult.StatusCode ?? -1);

The output is

400
-1
Skim answered 18/9, 2024 at 12:22 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.