ASP.Net WebAPI:如何使用IHttpActionResult而不是HttpResponseMessage

时间:2018-05-26 15:17:25

标签: c# asp.net-web-api

我想使用IHttpActionResult代替HttpResponseMessage。在下面的代码中,我使用了HttpResponseMessage,但正在寻找一些指南,如何在代码下重组以使用IHttpActionResult return Ok()return NotFound();

请指导我如何重构我的下面的代码,因此我将能够使用Ok()返回数据列表。还告诉我如何在NotFound(); <找不到数据时返回错误消息/ p>

[System.Web.Http.HttpGet, System.Web.Http.Route("UserAppointments/{email}")]
public System.Net.Http.HttpResponseMessage UserAppointments(string email = null)
{
    System.Net.Http.HttpResponseMessage retObject = null;

    if (!string.IsNullOrEmpty(email))
    {
        UserAppointmentService _appservice = new UserAppointmentService();
        IEnumerable<Entities.UserAppointments> app = _appservice.GetAppointmentsByEmail(email);

        if (app.Count() <= 0)
        {
            var message = string.Format("No appointment found for the user [{0}]", email);
            HttpError err = new HttpError(message);
            retObject = Request.CreateErrorResponse(System.Net.HttpStatusCode.NotFound, err);
            retObject.ReasonPhrase = message;
        }
        else
        {
            retObject = Request.CreateResponse(System.Net.HttpStatusCode.OK, app);
        }
    }
    else
    {
        var message = string.Format("No email provided");
        HttpError err = new HttpError(message);
        retObject = Request.CreateErrorResponse(System.Net.HttpStatusCode.NotFound, err);
        retObject.ReasonPhrase = message;

    }
    return retObject;
}

2 个答案:

答案 0 :(得分:1)

有两种方法可以解决这个问题

首先,通过更改返回类型并将HttpResponseMessage传递给ResponseMessage来返回IHttpActionResult派生类是很简单的。

[HttpGet]
[Route("UserAppointments/{email}")]
public IHttpActionResult UserAppointments(string email = null) {
    HttpResponseMessage retObject = null;    
    if (!string.IsNullOrEmpty(email)) {
        UserAppointmentService _appservice = new UserAppointmentService();
        IEnumerable<Entities.UserAppointments> app = _appservice.GetAppointmentsByEmail(email);

        if (app.Count() <= 0) {
            var message = string.Format("No appointment found for the user [{0}]", email);
            HttpError err = new HttpError(message);
            retObject = Request.CreateErrorResponse(System.Net.HttpStatusCode.NotFound, err);
            retObject.ReasonPhrase = message;
        } else {
            retObject = Request.CreateResponse(System.Net.HttpStatusCode.OK, app);
        }
    } else {
        var message = string.Format("No email provided");
        HttpError err = new HttpError(message);
        retObject = Request.CreateErrorResponse(System.Net.HttpStatusCode.NotFound, err);
        retObject.ReasonPhrase = message;

    }
    return ResponseMessage(retObject);
}

另一种方法是重构该方法以遵循Asp.Net Web API 2文档中的语法建议。

[HttpGet]
[Route("UserAppointments/{email}")]
public IHttpActionResult UserAppointments(string email = null) {
    if (!string.IsNullOrEmpty(email)) {
        var _appservice = new UserAppointmentService();
        IEnumerable<Entities.UserAppointments> app = _appservice.GetAppointmentsByEmail(email);
        if (app.Count() <= 0) {
            var message = string.Format("No appointment found for the user [{0}]", email);
            return Content(HttpStatusCode.NotFound, message);
        }
        return Ok(app);
    }
    return BadRequest("No email provided");
}

参考Action Results in Web API 2

答案 1 :(得分:1)

请尝试以下方法:

[HttpGet("{email}")]
[Route("UserAppointments/{email}")]
public IHttpActionResult GetAppoinment(string email = null){
    if (!string.IsNullOrEmpty(email)){
        //Your business logic ...
        List<UserAppointment> app = new List<UserAppointment>();
        if (app.Count() <= 0)
            return Content(HttpStatusCode.NoContent, string.Format("No appointment found for the user [{0}]", email));
        else
            return Ok(app);
    } else {
        return BadRequest("No email provided");
    }
}

进一步阅读: