0

在我的 Application_Start 中:

var jsonFormatter = new JsonMediaTypeFormatter();
config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));

我的默认网址:

[HttpGet]
[Route("~/")]
public HttpResponseMessage Index()
{
    var stream = File.OpenRead(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Views\Home\Index.html"));
    var content = new StreamContent(stream);
    return new HttpResponseMessage() { Content = content };
}

内容是类型"text/html",但我没有在其中设置它,response.Headers.ContentType但仍然正确返回了 html 文件,尽管没有类似 html 内容协商器之类的东西,实际上我假设该操作会将 html 文件作为 json 返回,否则会发生错误但一切正常。

这是为什么?

4

1 回答 1

0

谷歌搜索了一段时间后,我发现了这个解释:

http://www.w3.org/Protocols/rfc2616/rfc2616-sec7.html#sec7.2.1

任何包含实体主体的 HTTP/1.1 消息都应该包含定义该主体的媒体类型的 Content-Type 头字段。当且仅当媒体类型不是由 Content-Type 字段给出时,接收者可以尝试通过检查其内容和/或用于识别资源的 URI 的名称扩展来猜测媒体类型。如果媒体类型仍然未知,接收者应该将其视为类型“application/octet-stream”。

所以我的“浏览器”正在检查以 .HTML 结尾的资源的 URI,因此它被视为内容类型“text/html”:-)

于 2014-01-06T17:38:27.693 回答