![](/img/trans.png)
[英]How to return http error status code along with custom message in a class
[英]How to return Custom HTTP Status code + message in IEnumerable<xxx.Models.xxx> function
public IEnumerable<xxx.Models.Product> ReviewAll(string para1, string para2, string para3, int para4)
{
return //HTTPStatusCode
}
目前我正在使用它
int HTTPResponse=400;
return Request.CreateResponse((HttpStatusCode)HTTPResponse, "InvalidID");
但這會返回一個錯誤說
無法將類型'System.Net.Http.HttpResponseMessage'隱式轉換為'System.Collections.Generic.IEnumerable'。 存在顯式轉換(您是否錯過了演員?)
怎么解決這個?
重構動作以返回HttpResponseMessage
public HttpResponseMessage ReviewAll(string para1, string para2, string para3, int para4) {
if(some_condition) {
//...code removed for brevity
//if request is invalid then return appropriate status response
int HTTPResponse = 400;
var response = Request.CreateResponse((HttpStatusCode)HTTPResponse);
response.ReasonPhrase = "InvalidID";
return response;
} else {
//If the data is valid I need to return the data segment,
IEnumerable<xxx.Models.Product> responseBody = //...code removed for brevity
return Request.CreateResponse(HttpStatusCode.Ok, responseBody);//include data with an HttpStatus.Ok (200) response
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.