0

I implemented compression per the solution here:

Compress HTTP GET Response

However, my Delete Web API is throwing an exception:

public HttpResponseMessage Delete(int id)
    {
        if (_repo == null)
        {
            _uow = DependencyResolver.Current.GetService<TPS.Data.Can.IUnitOfWork>();
            _repo = _uow.TradeSpendRepository;
        }
        if (!_repo.Delete(id))
        {
            return Request.CreateResponse(HttpStatusCode.NotFound);
        }
        _uow.Save();
        return Request.CreateResponse(HttpStatusCode.OK);
    }

The exception is thrown in the CompressedContent's constructor because content is null:

if (content == null)
{
   throw new ArgumentNullException("content");
}

I guess returning a status code isn't enough! What's the best approach to prevent this exception?

Community
  • 1
  • 1
Mister Epic
  • 16,295
  • 13
  • 76
  • 147

1 Answers1

0

Since there is no content here, there is no need of creating a CompressedContent here, so you should add an additional check in the message handler.

Example:

if (response.Content != null && response.RequestMessage.Headers.AcceptEncoding != null)
{
    string encodingType = response.RequestMessage.Headers.AcceptEncoding.First().Value;

    response.Content = new CompressedContent(response.Content, encodingType);
}
Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
Kiran
  • 56,921
  • 15
  • 176
  • 161