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?
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") });
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With