You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

BadRequestResult与BadRequestObjectResult的区别及ActionFilter选型指引

Awesome question—this is a common point of confusion when working with ASP.NET Core action results, especially since the pattern repeats across nearly all status code types. Let's break this down clearly.

Core Differences Between XxxResult and XxxObjectResult

The pairings like BadRequestResult/BadRequestObjectResult or NotFoundResult/NotFoundObjectResult follow the exact same logic:

  • XxxResult (e.g., BadRequestResult)

    This is a status-only response. It sends back the appropriate HTTP status code (like 400 for BadRequest) but includes no response body content. The client knows the request failed, but gets no details about why.

    Example usage:

    // Returns HTTP 400 with an empty response body
    context.Result = new BadRequestResult();
    
  • XxxObjectResult (e.g., BadRequestObjectResult)

    This extends the basic status code response by adding a serialized response body. You pass an object (a string, model, error collection, etc.) to the constructor, and ASP.NET Core will serialize it (usually to JSON) and include it in the response alongside the status code.

    Example usage:

    // Returns HTTP 400 with a JSON body: { "Message": "Invalid email format" }
    context.Result = new BadRequestObjectResult("Invalid email format");
    
    // Or return structured error details (great for form validation)
    var validationErrors = context.ModelState.Values
        .SelectMany(v => v.Errors.Select(e => e.ErrorMessage));
    context.Result = new BadRequestObjectResult(new {
        StatusCode = 400,
        Message = "Request contains invalid data",
        Errors = validationErrors
    });
    
How to Choose Between Them

The decision boils down to whether you need to communicate additional context to the client:

  1. Use XxxResult when:

    • You only need to signal the status of the request (e.g., "this request is invalid" or "the resource doesn't exist") without explaining why.
    • The client already knows the failure reason (e.g., it's a generic validation catch-all where details aren't necessary).
  2. Use XxxObjectResult when:

    • You need to provide specific error details (like validation errors for form fields, missing required parameters, or a custom message explaining the failure).
    • The client needs actionable information to fix the request (e.g., a mobile app showing a user-friendly error message, or an API consumer debugging their request).
This Pattern Applies to All Status Code Pairs

Just to confirm, this same logic applies to every matching pair in ASP.NET Core:

  • OkResult (200, no body) vs OkObjectResult (200 with body)
  • NotFoundResult (404, no body) vs NotFoundObjectResult (404 with body explaining what's missing)
  • UnauthorizedResult (401, no body) vs UnauthorizedObjectResult (401 with context like "Invalid API key")

内容的提问来源于stack exchange,提问作者Mohammad Barbast

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 12:17:46