【问题标题】:Web APi Void, IIS express content type, IIS no content TypeWeb APi Void,IIS express 内容类型,IIS 无内容类型
【发布时间】:2013-10-10 19:47:31
【问题描述】:

好的,所以我有一个带有 put 操作和返回类型 void 的 web api 控制器。当我使用 VS 的内置 iiepxress 运行它并调用它时,我得到了预期的 204。这是标题:

Cache-Control   no-cache
Connection  close
Content-Type    text/html
Date    Thu, 10 Oct 2013 19:33:43 GMT
Expires -1
Pragma  no-cache
Server  Microsoft-IIS/8.0
X-AspNet-Version    4.0.30319
X-Powered-By    ASP.NET

当我将完全相同的代码放入我们的 sbx 环境时,我得到一个 204,但带有以下标头:

Cache-Control   no-cache
Date    Thu, 10 Oct 2013 19:39:59 GMT
Expires -1
Pragma  no-cache
Server  Microsoft-IIS/7.5
X-AspNet-Version    4.0.30319
X-Identifier    17253
X-Powered-By    ASP.NET

相关的区别是第二个中缺少 contentType。

这造成的问题是,在 firefox(我认为是 IE)中,它默认为 xml,尝试解析它并失败。

我知道如何通过在我的 web api 控制器中设置我的 contentType 来解决这个问题,但这对我来说似乎不是最好的解决方法。

那么,我要问的是 IIS 中的设置差异可能会导致这种情况吗?

谢谢

注意: 我的网址看起来像这样 /foo/bar/2 所以它不是 mimetype。

【问题讨论】:

    标签: c# iis asp.net-web-api


    【解决方案1】:

    如果您的服务以 204 响应,则响应不应包含消息正文。 This is by spec。我只能假设您在邮件正文中回复了一些内容。

    API 方法的响应应该是这样的:

    return new HttpResponseMessage { StatusCode = System.Net.HttpStatusCode.NoContent }
    

    编辑。我注意到您提到您返回“无效”。您的方法应该返回带有我上面提到的 StatusCode 的 HttpResponseMessage。

    【讨论】:

      【解决方案2】:

      这将解决问题:

          protected internal virtual IHttpActionResult NoContent()
          {
              HttpResponseMessage responseMsg = new HttpResponseMessage(HttpStatusCode.NoContent) {Content = new StringContent(string.Empty, Encoding.UTF8)};
              return this.ResponseMessage(responseMsg);
          }
      

      但仍然没有解释为什么 IIS 默认添加: 内容类型 text/html

      或者更好的方法是使用 web.config 或 IIS 配置删除它。

      【讨论】:

        【解决方案3】:

        我用这个:

        返回状态码(HttpStatusCode.NoContent);

        【讨论】:

          猜你喜欢
          • 2016-02-05
          • 2012-04-25
          • 1970-01-01
          • 2018-08-16
          • 2011-01-05
          • 1970-01-01
          • 2016-03-10
          • 2013-09-05
          • 1970-01-01
          相关资源
          最近更新 更多