WebAPI2返回记录列表并请求授权令牌

时间:2013-11-06 20:09:27

标签: http-headers asp.net-web-api response-headers

我不太清楚如何处理这样的情况。

我有以下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;有效??

1 个答案:

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