【问题标题】:Compressed Delete Web API throwing exception压缩删除 Web API 抛出异常
【发布时间】:2013-07-22 16:20:29
【问题描述】:

我在这里按照解决方案实施了压缩:

Compress HTTP GET Response

但是,我的 Delete Web API 抛出异常:

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);
    }

在 CompressedContent 的构造函数中抛出异常,因为 content 为空:

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

我猜返回状态码是不够的!防止此异常的最佳方法是什么?

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-web-api


    【解决方案1】:

    由于此处没有内容,因此无需在此处创建CompressedContent,因此您应该在消息处理程序中添加额外的检查。

    例子:

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

    【讨论】:

      猜你喜欢
      • 2013-01-02
      • 2015-04-15
      • 1970-01-01
      • 1970-01-01
      • 2014-05-15
      • 2011-08-28
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多