我不太清楚如何处理这样的情况。
我有以下API
public IEnumerable<Contact> GetContactList()
{
pseudo code >>
if AuthenticationToken from Header NOT present
return HttpResponseMessage(HttpStatusCode.BadRequest ,
"Authentication Token missing");
return list of contacts from DB;
}
是否有API在相同的过程中返回HttpResponseMessage或IEnumerable列表&gt;&gt;有效??
答案 0 :(得分:1)
如果您将返回类型更改为HttpResponseMessage,则可以执行以下操作:
public HttpResponseMessage GetContactList()
{
if (IsValid(Request.Headers.Authorization))
{
var contacts = DB.GetContacts();
return Request.CreateResponse(HttpStatusCode.OK, contacts);
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest, "Authentication Token missing");
}
}