我正在开发一组 REST 服务,我们打算同时支持 JSON 和 BSON,可能还有 XML,作为返回数据的任何资源调用的返回类型。让 application/json 正常工作是很容易的,但是当托管在 IIS 7.x 中时,我们遇到了来自 IIS 本身的默认 406 错误,该错误指出只有 application/json 可以用于我们的路由。需要注意的是,我们的路由都是少扩展的,比如:
http://localhost/SomeEntity/12345
我们更愿意根据客户端 HTTP Accept 标头中指定的最喜欢的内容类型来序列化数据。如果指定了 text/xml 或 application/xml,我们将发送 xml。如果指定了 application/bson,我们将发送 BSON 等。最终,我们希望支持任何内容类型,如果需要,将来允许使用协议缓冲区或其他一些高速序列化方法。
有谁知道为什么 IIS 7 似乎将 application/json 硬编码为可以由无扩展资源 URL 返回的唯一可接受的内容类型?
我已经深入研究了服务器级别以及每个站点级别的 Mime Types 设置。我已经为. 扩展名,以及 .* 扩展名。似乎不可能简单地为 *. 老实说,我什至无法弄清楚 IIS 7 一开始是如何路由请求的。ExtensionlessUrlHandler-Integrated-4.0 似乎不是这样,因为它不允许 PUT 动词通过,但 PUT 动词可以正常工作。我不确定它是否可能通过模块,例如 UrlRoutingModule-4.0。我完全不知道 IIS 7 是如何处理 ASP.NET MVC 3 路由的,它们为什么工作,或者我可以在哪里配置允许无扩展请求的内容类型。我宁愿允许/,而不是仅限于任何显式的 mime 类型集,但是如果需要显式集,如果我能做到这一点,我会很乐意这样做。
例子:
请求
GET http://some.host.com/Resource/1234 HTTP/1.1
主机:internalinterop-course-resources.ecollege-labs.com
接受:application/json
GET http://some.host.com/Resource/1234 HTTP/1.1
主机:internalinterop-course-resources.ecollege-labs.com
响应
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/json; charset=utf-8
服务器:Microsoft-IIS/7.5
X-AspNetMvc-Version:3.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
日期:2012 年 2 月 29 日星期三 23:34:38 GMT
内容长度:??
{"消息":"yadda yadda"}
请求
GET http://some.host.com/Resource/1234 HTTP/1.1
主机:internalinterop-course-resources.ecollege-labs.com
接受:application/xml
回复
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>IIS 7.5 Detailed Error - 406.0 - Not Acceptable; Supported content types: , application/json</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana,Arial,Helvetica,sans-serif;background:#CBE1EF;}
code{margin:0;color:#006600;font-size:1.1em;font-weight:bold;}
.config_source code{font-size:.8em;color:#000000;}
pre{margin:0;font-size:1.4em;word-wrap:break-word;}
ul,ol{margin:10px 0 10px 40px;}
ul.first,ol.first{margin-top:5px;}
fieldset{padding:0 15px 10px 15px;}
.summary-container fieldset{padding-bottom:5px;margin-top:4px;}
legend.no-expand-all{padding:2px 15px 4px 10px;margin:0 0 0 -12px;}
legend{color:#333333;padding:4px 15px 4px 10px;margin:4px 0 8px -12px;_margin-top:0px;
border-top:1px solid #EDEDED;border-left:1px solid #EDEDED;border-right:1px solid #969696;
border-bottom:1px solid #969696;background:#E7ECF0;font-weight:bold;font-size:1em;}
a:link,a:visited{color:#007EFF;font-weight:bold;}
a:hover{text-decoration:none;}
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;}
h3{font-size:1.4em;margin:10px 0 0 0;color:#CC0000;}
h4{font-size:1.2em;margin:10px 0 5px 0;
}#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS",Verdana,sans-serif;
color:#FFF;background-color:#5C87B2;
}#content{margin:0 0 0 2%;position:relative;}
.summary-container,.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
.config_source{background:#fff5c4;}
.content-container p{margin:0 0 10px 0;
}#details-left{width:35%;float:left;margin-right:2%;
}#details-right{width:63%;float:left;overflow:hidden;
}#server_version{width:96%;_height:1px;min-height:1px;margin:0 0 5px 0;padding:11px 2% 8px 2%;color:#FFFFFF;
background-color:#5A7FA5;border-bottom:1px solid #C1CFDD;border-top:1px solid #4A6C8E;font-weight:normal;
font-size:1em;color:#FFF;text-align:right;
}#server_version p{margin:5px 0;}
table{margin:4px 0 4px 0;width:100%;border:none;}
td,th{vertical-align:top;padding:3px 0;text-align:left;font-weight:bold;border:none;}
th{width:30%;text-align:right;padding-right:2%;font-weight:normal;}
thead th{background-color:#ebebeb;width:25%;
}#details-right th{width:20%;}
table tr.alt td,table tr.alt th{background-color:#ebebeb;}
.highlight-code{color:#CC0000;font-weight:bold;font-style:italic;}
.clear{clear:both;}
.preferred{padding:0 5px 2px 5px;font-weight:normal;background:#006633;color:#FFF;font-size:.8em;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error in Application "some.host.com"</h1></div>
<div id="server_version"><p>Internet Information Services 7.5</p></div>
<div id="content">
<div class="content-container">
<fieldset><legend>Error Summary</legend>
<h2>HTTP Error 406.0 - Not Acceptable; Supported content types: , application/json</h2>
<h3>The resource cannot be displayed because the file extension is not being accepted by your browser.</h3>
</fieldset>
</div>
<div class="content-container">
<fieldset><legend>Detailed Error Information</legend>
<div id="details-left">
<table border="0" cellpadding="0" cellspacing="0">
<tr class="alt"><th>Module</th><td>ManagedPipelineHandler</td></tr>
<tr><th>Notification</th><td>ExecuteRequestHandler</td></tr>
<tr class="alt"><th>Handler</th><td>System.Web.Mvc.MvcHandler</td></tr>
<tr><th>Error Code</th><td>0x00000000</td></tr>
</table>
</div>
<div id="details-right">
<table border="0" cellpadding="0" cellspacing="0">
<tr class="alt"><th>Requested URL</th><td>http://some.host.com:80/Resource/1234</td></tr>
<tr><th>Physical Path</th><td>x:\inetpub\some.host.com\Resources\1234</td></tr>
<tr class="alt"><th>Logon Method</th><td>Anonymous</td></tr>
<tr><th>Logon User</th><td>Anonymous</td></tr>
</table>
<div class="clear"></div>
</div>
</fieldset>
</div>
<div class="content-container">
<fieldset><legend>Most likely causes:</legend>
<ul> <li>The request was rejected because it contained an Accept header for a MIME type that is not supported for the requested file extension.</li> </ul>
</fieldset>
</div>
<div class="content-container">
<fieldset><legend>Things you can try:</legend>
<ul> <li>Verify the MIME settings for the file extension that was requested to make sure this MIME type is acceptable.</li> </ul>
</fieldset>
</div>
<div class="content-container">
<fieldset><legend>Links and More Information</legend>
This error occurs when the client requests a file with a certain file extension and then specifies the Accept header with a MIME type that is different from the configuration of this file extension on the server. An example is requesting a file with the .doc extension and specifying an Accept header with text/xml instead of application/msword. Usually the client specifies */* for the Accept header, which allows the request to serve any MIME type.
<p><a href="http://go.microsoft.com/fwlink/?LinkID=62293&IIS70Error=406,0,0x00000000,7601">View more information »</a></p>
</fieldset>
</div>
</div>
</body>
</html>