如何在 IEnumerable<xxx.Models.xxx> 函数中 return 自定义 HTTP 状态代码 + 消息
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");
但是这个returns一个错误说
Cannot implicitly convert type 'System.Net.Http.HttpResponseMessage'
to 'System.Collections.Generic.IEnumerable'. An
explicit conversion exists (are you missing a cast?)
如何解决这个问题??
将操作重构为 return 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
}
}
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");
但是这个returns一个错误说
Cannot implicitly convert type 'System.Net.Http.HttpResponseMessage' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)
如何解决这个问题??
将操作重构为 return 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
}
}