Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I return a HttpStatusCode from an IQueryable<T>?

I am coding a C# WebApi 2 webservice, and I have a question in regards to returning a HttpStatusCode from an IQueryable<T> webservice function.

If a web service function returns a single object, the following can easily be used:

public async Task<IHttpActionResult> GetItem(int id)
{
    return Content(HttpStatusCode.Unauthorized, "Any object");
}

In the following situation, I am not sure on how to return a specified HttpStatusCode:

public IQueryable<T> GetItems()
{
    return Content(HttpStatusCode.Unauthorized, "Any object");
}

Can I please have some help with this?

like image 541
user3736648 Avatar asked Feb 09 '16 10:02

user3736648


1 Answers

You can throw a HttpResponseException with the appropriate statuscode

public IQueryable<T> GetItems()
{
   throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Unauthorized) { Content = new StringContent("Any object") });
}
like image 169
Jehof Avatar answered Sep 27 '22 22:09

Jehof