Returning http status code from Web Api controller
Asked Answered
A

14

262

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.

Allayne answered 18/5, 2012 at 15:20 Comment(4)
Are you using beta or nightly build?Eulogist
@Eulogist I'm using betaAllayne
so what is wrong with returning new HttpResponseMessage(HttpStatusCode.NotModified) ? Does it not work?Eulogist
@Eulogist I can't return HttpResponseMessage when the return type is User, it's not compiling (obviously).Allayne
E
290

I did not know the answer so asked the ASP.NET team here.

So the trick is to change the signature to HttpResponseMessage and use Request.CreateResponse.

[ResponseType(typeof(User))]
public HttpResponseMessage GetUser(HttpRequestMessage request, int userId, DateTime lastModifiedAtClient)
{
    var user = new DataEntities().Users.First(p => p.Id == userId);
    if (user.LastModified <= lastModifiedAtClient)
    {
         return new HttpResponseMessage(HttpStatusCode.NotModified);
    }
    return request.CreateResponse(HttpStatusCode.OK, user);
}
Eulogist answered 18/5, 2012 at 19:25 Comment(13)
It doesn't compile in ASP.NET MVC 4 beta release, as CreateResponse takes only status code as parameter. secondly I wanted a solution with no HttpResponseMessage as the return value as it's being deprecated: aspnetwebstack.codeplex.com/discussions/350492Allayne
@Allayne HttpResponseMessage is not being deprecated, generic HttpResponseMessage is. I have not used the generic one here.Eulogist
I know. But beta is heavily changing. If you are investing anything you got to use newer stuff. But if you insist you can go back to using generic HttpResponseMessage. You just cannot have the cake and eat it at the same time.Eulogist
is there a stable version after the Beta? relying on Beta in our production is one thing, but relying on nightly builds that are not stable is quite another thing...Allayne
Also do you know what are the major differences between the beta and the currently nightly build?Allayne
In case anyone needs it, to get the value from the controller method would be GetUser(request, id, lastModified).TryGetContentValue(out user), where user (in the example case) is a User object.Herbivore
added ResponseType attribute to denote what the response content type isOptometry
Is this still the preferred method in 2015? MVC 5?Sibella
@Sibella API in 5 looks a bit different. codethug.com/2015/01/23/web-api-http-response-codes (The example is wrong however, it's return NotFound(...) not NotFoundResult.Jotunheim
I came up with another solution built from this solution that doesn't force you to change your method signature.Dinnie
The more modern version returns IHttpActionResult - not HttpResponseMessage (2017)Jenicejeniece
To add to niico's suggestion, when the return type is IHttpActionResult and you want to return the User, you can just do return Ok(user). If you need to return another status code (say, forbidden) you can just do return this.StatusCode(HttpStatusCode.Forbidden).Dedal
Happy with the solution but instead of passing request as a parameter, I will go with Luke Puplett solution below using this.RequestVapory
H
68

You can also do the following if you want to preserve the action signature as returning User:

public User GetUser(int userId, DateTime lastModifiedAtClient) 

If you want to return something other than 200 then you throw an HttpResponseException in your action and pass in the HttpResponseMessage you want to send to the client.

Holmun answered 15/6, 2012 at 20:41 Comment(4)
This is a way more elegant solution (albiet incomplete answer). Why is everyone preferring to do it the hard way?Jotunheim
@Geoist #1282752. Throwing exception is costly.Mayan
Yeah, if you're designing a busy API, using an exception to communicate the most common case of NotModified is really wasteful. If all your APIs did this, then your server will be mostly converting watts to exceptions.Davena
@Jotunheim because you can't return a custom error message if you throw an error (like a 400 response)... also throwing exceptions is silly for something that you expect the code to do. Expensive and will be logged when you don't neccesarily want them to be. They're not really exceptions.Fonville
L
45

In MVC 5, things got easier:

return new StatusCodeResult(HttpStatusCode.NotModified, this);
Livialivid answered 1/4, 2015 at 10:57 Comment(3)
Can't specify a message?Sibella
Using a message is actually the accepted answer. This is just a little terserLivialivid
So no, you can't specify a message with the StatusCodeResult.Cribriform
D
44

Change the GetXxx API method to return HttpResponseMessage and then return a typed version for the full response and the untyped version for the NotModified response.

    public HttpResponseMessage GetComputingDevice(string id)
    {
        ComputingDevice computingDevice =
            _db.Devices.OfType<ComputingDevice>()
                .SingleOrDefault(c => c.AssetId == id);

        if (computingDevice == null)
        {
            return this.Request.CreateResponse(HttpStatusCode.NotFound);
        }

        if (this.Request.ClientHasStaleData(computingDevice.ModifiedDate))
        {
            return this.Request.CreateResponse<ComputingDevice>(
                HttpStatusCode.OK, computingDevice);
        }
        else
        {
            return this.Request.CreateResponse(HttpStatusCode.NotModified);
        }
    }

*The ClientHasStale data is my extension for checking ETag and IfModifiedSince headers.

The MVC framework should still serialize and return your object.

NOTE

I think the generic version is being removed in some future version of the Web API.

Davena answered 4/7, 2012 at 11:31 Comment(2)
This was the exact answer I was looking for -- albeit as a Task<HttpResponseMessage<T>> return type. Thanks!Equitant
@Equitant - yes, that's totally worth calling out. More info on async here asp.net/mvc/tutorials/mvc-4/…Davena
E
25

For ASP.NET Web Api 2, this post from MS suggests to change the method's return type to IHttpActionResult. You can then return a built in IHttpActionResult implementation like Ok, BadRequest, etc (see here) or return your own implementation.

For your code, it could be done like:

public IHttpActionResult GetUser(int userId, DateTime lastModifiedAtClient)
{
    var user = new DataEntities().Users.First(p => p.Id == userId);
    if (user.LastModified <= lastModifiedAtClient)
    {
        return StatusCode(HttpStatusCode.NotModified);
    }
    return Ok(user);
}
Earthshaker answered 26/4, 2019 at 18:39 Comment(0)
N
22

.net core 2.2 returning 304 status code. This is using an ApiController.

    [HttpGet]
    public ActionResult<YOUROBJECT> Get()
    {
        return StatusCode(304);
    }

Optionally you can return an object with the response

    [HttpGet]
    public ActionResult<YOUROBJECT> Get()
    {
        return StatusCode(304, YOUROBJECT); 
    }
Najera answered 2/1, 2019 at 11:10 Comment(0)
L
21

I hate bumping old articles but this is the first result for this in google search and I had a heck of a time with this problem (even with the support of you guys). So here goes nothing...

Hopefully my solution will help those that also was confused.

namespace MyApplication.WebAPI.Controllers
{
    public class BaseController : ApiController
    {
        public T SendResponse<T>(T response, HttpStatusCode statusCode = HttpStatusCode.OK)
        {
            if (statusCode != HttpStatusCode.OK)
            {
                // leave it up to microsoft to make this way more complicated than it needs to be
                // seriously i used to be able to just set the status and leave it at that but nooo... now 
                // i need to throw an exception 
                var badResponse =
                    new HttpResponseMessage(statusCode)
                    {
                        Content =  new StringContent(JsonConvert.SerializeObject(response), Encoding.UTF8, "application/json")
                    };

                throw new HttpResponseException(badResponse);
            }
            return response;
        }
    }
}

and then just inherit from the BaseController

[RoutePrefix("api/devicemanagement")]
public class DeviceManagementController : BaseController
{...

and then using it

[HttpGet]
[Route("device/search/{property}/{value}")]
public SearchForDeviceResponse SearchForDevice(string property, string value)
{
    //todo: limit search property here?
    var response = new SearchForDeviceResponse();

    var results = _deviceManagementBusiness.SearchForDevices(property, value);

    response.Success = true;
    response.Data = results;

    var statusCode = results == null || !results.Any() ? HttpStatusCode.NoContent : HttpStatusCode.OK;

    return SendResponse(response, statusCode);
}
Lanceolate answered 15/8, 2017 at 15:38 Comment(1)
Brilliant. Saved me a ton of time.Salbu
S
7

Try this :

return new ContentResult() { 
    StatusCode = 404, 
    Content = "Not found" 
};
Schechinger answered 4/10, 2019 at 19:28 Comment(0)
D
5

I don't like having to change my signature to use the HttpCreateResponse type, so I came up with a little bit of an extended solution to hide that.

public class HttpActionResult : IHttpActionResult
{
    public HttpActionResult(HttpRequestMessage request) : this(request, HttpStatusCode.OK)
    {
    }

    public HttpActionResult(HttpRequestMessage request, HttpStatusCode code) : this(request, code, null)
    {
    }

    public HttpActionResult(HttpRequestMessage request, HttpStatusCode code, object result)
    {
        Request = request;
        Code = code;
        Result = result;
    }

    public HttpRequestMessage Request { get; }
    public HttpStatusCode Code { get; }
    public object Result { get; }

    public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
    {
        return Task.FromResult(Request.CreateResponse(Code, Result));
    }
}

You can then add a method to your ApiController (or better your base controller) like this:

protected IHttpActionResult CustomResult(HttpStatusCode code, object data) 
{
    // Request here is the property on the controller.
    return new HttpActionResult(Request, code, data);
}

Then you can return it just like any of the built in methods:

[HttpPost]
public IHttpActionResult Post(Model model)
{
    return model.Id == 1 ?
                Ok() :
                CustomResult(HttpStatusCode.NotAcceptable, new { 
                    data = model, 
                    error = "The ID needs to be 1." 
                });
}
Dinnie answered 26/1, 2017 at 20:1 Comment(1)
Might want to use HttpStatusCode.BadRequest (400) in your example. HttpStatusCode.NotAcceptable (406) is used when content negotiation/accept headers specify something that is not accepted by the server.Schechinger
A
4

If you need to return an IHttpActionResult and want to return the error code plus a message, use:

return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.NotModified, "Error message here"));
Amphictyony answered 29/7, 2016 at 5:55 Comment(0)
I
4

Another option:

return new NotModified();

public class NotModified : IHttpActionResult
{
    public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
    {
        var response = new HttpResponseMessage(HttpStatusCode.NotModified);
        return Task.FromResult(response);
    }
}
Insolvency answered 15/11, 2016 at 13:56 Comment(0)
L
2
public HttpResponseMessage Post(Article article)
{
    HttpResponseMessage response = Request.CreateResponse<Article>(HttpStatusCode.Created, article);

    string uriToTheCreatedItem = Url.Route(null, new { id = article.Id });
    response.Headers.Location = new Uri(Request.RequestUri, uriToTheCreatedItem);

    return response;
}
Leading answered 8/7, 2015 at 21:32 Comment(0)
H
2

An update to @Aliostads answer using the more moden IHttpActionResult introduced in Web API 2.

https://learn.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/action-results#ihttpactionresult

public class TryController : ApiController
{
    public IHttpActionResult GetUser(int userId, DateTime lastModifiedAtClient)
    {
        var user = new DataEntities().Users.First(p => p.Id == userId);
        if (user.LastModified <= lastModifiedAtClient)
        {
            return StatusCode(HttpStatusCode.NotModified);
            // If you would like to return a Http Status code with any object instead:
            // return Content(HttpStatusCode.InternalServerError, "My Message");
        }
        return Ok(user);
    }
}
Haematinic answered 24/4, 2019 at 9:36 Comment(0)
H
0

I know there are several good answers here but this is what I needed so I figured I'd add this code in case anyone else needs to return whatever status code and response body they wanted in 4.7.x with webAPI.

public class DuplicateResponseResult<TResponse> : IHttpActionResult
{
    private TResponse _response;
    private HttpStatusCode _statusCode;
    private HttpRequestMessage _httpRequestMessage;
    public DuplicateResponseResult(HttpRequestMessage httpRequestMessage, TResponse response, HttpStatusCode statusCode)
    {
        _httpRequestMessage = httpRequestMessage;
        _response = response;
        _statusCode = statusCode;
    }

    public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
    {
        var response = new HttpResponseMessage(_statusCode);
        return Task.FromResult(_httpRequestMessage.CreateResponse(_statusCode, _response));
    }
}
Helm answered 4/1, 2021 at 21:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.