[英]Why should I use IHttpActionResult instead of HttpResponseMessage?
[英]ASP.Net WebAPI: How to use IHttpActionResult instead of HttpResponseMessage
我想使用IHttpActionResult
而不是HttpResponseMessage
。 在下面的代碼中,我使用了HttpResponseMessage
但在尋找一些指導方針如何重組下面的代碼以將IHttpActionResult
與return Ok()
和return 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;
}
有兩種方法可以解決這個問題
第一個方法很簡單,只需更改返回類型並將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");
}
請嘗試以下操作:
[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");
}
}
進一步閱讀:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.