简体   繁体   English

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

[英]ASP.Net WebAPI: How to use IHttpActionResult instead of HttpResponseMessage

I would like to use IHttpActionResult instead of HttpResponseMessage . 我想使用IHttpActionResult而不是HttpResponseMessage In the below code I used HttpResponseMessage but looking for some guide line how to restructure below code to use IHttpActionResult with return Ok() and return NotFound(); 在下面的代码中,我使用了HttpResponseMessage但在寻找一些指导方针如何重组下面的代码以将IHttpActionResultreturn Ok()return NotFound();

Please guide me how to restructure my below code as a result I will be able to return list of data with Ok() also tell me how could I return error message when data not found with NotFound(); 请指导我如何重组下面的代码,这样我将能够使用Ok()返回数据列表,同时告诉我在NotFound();找不到数据时如何返回错误消息NotFound();

[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;
}

There are two ways to deal with this 有两种方法可以解决这个问题

First one is simple by changing the return type and passing the HttpResponseMessage to ResponseMessage which returns a IHttpActionResult derived class. 第一个方法很简单,只需更改返回类型并将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);
}

The alternative is to refactor the method to follow the syntax suggestions from Asp.Net Web API 2 documentation. 替代方法是重构该方法,以遵循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");
}

Reference Action Results in Web API 2 Web API 2中的参考操作结果

Please try the following: 请尝试以下操作:

[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");
    }
}

Further reading: 进一步阅读:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM