Returning http status code from Web Api controller

I’m trying to return a status code of 304 not modified for a GET method in a web api controller.

The only way I succeeded was something like this:

public class TryController : ApiController
{
    public User GetUser(int userId, DateTime lastModifiedAtClient)
    {
        var user = new DataEntities().Users.First(p => p.Id == userId);
        if (user.LastModified <= lastModifiedAtClient)
        {
             throw new HttpResponseException(HttpStatusCode.NotModified);
        }
        return user;
    }
}

The problem here is that it’s not an exception, It’s just not modified so the client cache is OK.
I also want the return type to be a User (as all the web api examples shows with GET) not return HttpResponseMessage or something like this.

14 Answers
14

Leave a Comment