How can I return an HttpStatusCode from an IQueryable <T>?
I am coding a C# WebApi 2 web service and I have a question about returning an HttpStatusCode from the webservice IQueryable<T> function.
If the web service function returns a single object, you can easily use the following:
public async Task<IHttpActionResult> GetItem(int id) { return Content(HttpStatusCode.Unauthorized, "Any object"); } In the following situation, I'm not sure how to return the specified HttpStatusCode :
public IQueryable<T> GetItems() { return Content(HttpStatusCode.Unauthorized, "Any object"); } Can i help with this?
In the first situation, you will still return an object in the Content method, for example:
public IHttpActionResult GetItem(int id) { return Content(HttpStatusCode.Unauthorized, "Any object"); } When this method is executed, you will see the "Any object" returned to the client. In Web API 2, IHttpActionResult is essentially a fancy factory around HttpResponseMessage . If you ignore that the return type of the method is IHttpActionResult , you can return IQueryable by replacing "Any object" with a query, for example:
public IHttpActionResult GetItem(int id) { IQueryable<object> results; return Ok(results); } More information about the results of actions in the Web API can be found here:
http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/action-results