Return 不使用 ControllerBase 的具有值的新 ActionResult

Return new ActionResult with value without using ControllerBase

我需要 return 一个包含对象的 ActionResult,但是,我没有继承自 ControllerBase,所以我无法使用 OkBadRequest等方法。我该怎么做?


更多信息:

我不从 ControllerBase 继承的原因是我拥有一个通用 Response 对象,它包含一个 return 是 ActionResult 的方法。确定哪个 ActionResult 应该returned 的逻辑将在此方法中。


我试过:


我的代码:

public enum ResponseStatus 
{
    Ok, 
    BadRequest,
    // ...
}

public class ResponseObject<T>
{
    public ResponseStatus Status { get; set; }
    public T Value { get; set; }

    public ActionResult ToActionResult()
    {
        switch (Status)
        {
            case ResponseStatus.Ok:
               return new SomethingThatIsOk(this); // this doesnt exist, I dont know to return, hence the question

            case ResponseStatus.BadRequest:
               return new SomethingThatIsBadRequest(this); // this doesnt exist, I dont know to return, hence the question

            // ...
        }
    }
}

有多个派生结果可用于获得所需的行为。

public class ResponseObject<T> {
    public ResponseStatus Status { get; set; }
    public T Value { get; set; }

    public ActionResult ToActionResult() {
        switch (Status) {
            case ResponseStatus.Ok:
               return new OkObjectResult(this);

            case ResponseStatus.BadRequest:
               return new BadRequestObjectResult(this);

            // ...
        }
    }
}

使用适合您所需状态的那个。

ObjectResult Class 有一些派生的 类 适用于这种情况,

Microsoft.AspNetCore.Mvc.AcceptedAtActionResult
Microsoft.AspNetCore.Mvc.AcceptedAtRouteResult
Microsoft.AspNetCore.Mvc.AcceptedResult
Microsoft.AspNetCore.Mvc.BadRequestObjectResult
Microsoft.AspNetCore.Mvc.ConflictObjectResult
Microsoft.AspNetCore.Mvc.CreatedAtActionResult
Microsoft.AspNetCore.Mvc.CreatedAtRouteResult
Microsoft.AspNetCore.Mvc.CreatedResult
Microsoft.AspNetCore.Mvc.NotFoundObjectResult
Microsoft.AspNetCore.Mvc.OkObjectResult
Microsoft.AspNetCore.Mvc.UnauthorizedObjectResult
Microsoft.AspNetCore.Mvc.UnprocessableEntityObjectResult
System.Web.Http.BadRequestErrorMessageResult
System.Web.Http.ExceptionResult
System.Web.Http.InvalidModelStateResult
System.Web.Http.NegotiatedContentResult<T>
System.Web.Http.ResponseMessageResult

但我建议您查看 API 文档,了解可能适合您需要的任何其他 类。

所有 ControllerBase 所做的就是将结果的创建包装在一个方法中

/// <summary>
/// Creates an <see cref="OkObjectResult"/> object that produces an <see cref="StatusCodes.Status200OK"/> response.
/// </summary>
/// <param name="value">The content value to format in the entity body.</param>
/// <returns>The created <see cref="OkObjectResult"/> for the response.</returns>
[NonAction]
public virtual OkObjectResult Ok([ActionResultObjectValue] object value)
    => new OkObjectResult(value);

Source

/// <summary>
/// Creates an <see cref="BadRequestObjectResult"/> that produces a <see cref="StatusCodes.Status400BadRequest"/> response.
/// </summary>
/// <param name="error">An error object to be returned to the client.</param>
/// <returns>The created <see cref="BadRequestObjectResult"/> for the response.</returns>
[NonAction]
public virtual BadRequestObjectResult BadRequest([ActionResultObjectValue] object error)
    => new BadRequestObjectResult(error);

Source