不使用ControllerBase

时间:2019-07-17 21:24:16

标签: c# .net-core actionresult

我需要返回一个包含对象的ActionResult,但是,我不继承自ControllerBase,因此无法使用OkBadRequest等。 方法。我该怎么办?


更多信息:

我不继承自ControllerBase的原因是我持有一个通用的Response对象,该对象包含一个返回ActionResult的方法。确定应返回哪个 ActionResult的逻辑将在此方法中。


我尝试过:

  • ContentResult
    这使我可以选择提供一个值,但只接受字符串Content的值。

  • StatusCodeResult
    这并没有给我提供提供用作值的对象的选项。

  • OkResultBadRequest等。
    这并没有给我指定值的选项。


我的代码:

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

            // ...
        }
    }
}

1 个答案:

答案 0 :(得分:1)

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

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