防止 Web API 添加 application/json 到 Content-Type header

Prevent Web API from adding application/json to Content-Type header

我有一个 .NET Web API 方法,它通过 MultipartContent return 多个文件。根据我初始化 MultipartContent 的方式,响应的 Content-Type 应该是 multipart/mixed; boundary="some_boundary"

    [HttpPost]
    public HttpResponseMessage DownloadFiles() {
        var content = new MultipartContent("mixed", "some_boundary");
        // add multiple StreamContent objects...

        return new HttpResponseMessage { Content = content, };
    }

然而,响应中的实际内容类型是 multipart/mixed; boundary="some_boundary",application/json。这有效吗?如何防止添加 ,application/json

我只需要修复这个方法,因为我的控制器还有其他方法可以执行 return JSON。我不想更改他们的回复 content-type。

如何防止添加 ,application/json

原来这是因为 Content-Type header 被 Web.config:

设置了
<system.webServer>
    <httpProtocol>
        <customHeaders>
            <add name="Content-Type" value="application/json"/>
        </customHeaders>
    </httpProtocol>
</system.webServer>

不确定为什么会有那条线,但我确定没有必要。删除该行后,我的回复返回了 content-type header of multipart/mixed; boundary="some_boundary" 这正是我想要的。

multipart/mixed; boundary="some_boundary",application/json 是有效的内容类型吗?

我相信答案是否定的,它是无效的,或者至少它会导致不可预测的行为。使用 .NET 作为客户端,content-type 的响应导致 HttpResponseMessage.Content.Headers.ContentType 为空,但是我仍然可以通过 HttpResponseMessage.Content.Headers.Where(h => h.Key == "Content-Type").

访问 header