简体   繁体   中英

How do I bypass Accept header validation in IIS?

I have a Nancy website hosted on ASP.Net with a number of custom routes. These routes include mappings which look like file paths (eg /path/to/xml_file.xml ), but that resolve to HTML files which should display in the browser. For most files, this works correctly. However, with XML files (and possibly other mime types), I am getting a 406.0 HTTP error from IIS 8 Express explicitly stating that

HTTP Error 406.0 - Not Acceptable

The resource cannot be displayed because the file extension is not being accepted by your browser.

The request was rejected because it contained an Accept header for a MIME type that is not supported for the requested file extension.

Verify the MIME settings for the file extension that was requested to make sure this MIME type is acceptable.

406错误页面截图

Is there any web.config setting or other technique for bypassing this validation so that I don't get this error without having to rewrite the path logic? My Google searching is failing me. For example, I have found this IIS forum post linked from the SO Answer which talks about

map all reqests to ASP.NET and use the ASP.NET's StaticFileHandler to serve them

However, the post does not explain how to do this; nor does it seem applicable.

Edit: (Added error page image above)

As another tidbit, the Nancy route returns an explicit view of the form

return View["view_name", myModel];

And therefore, should bypass content negotiation.

While not a fix or answer per se, my current work around (for anyone who might be interested) is to tweak the routes to use a trailing slash. As an example:

"/path/to/xml_file.xml"   //  Still returns a 406.0 error
"/path/to/xml_file.xml/"  //  Works properly (notice the trailing slash)

Here's a link to the Rackspace knowledge center that shows how to change the MIME mapping for static content in the web.config file.

https://www.iis.net/configreference/system.webserver/staticcontent/mimemap

There's also this one from MS that's more in-depth.

https://www.iis.net/configreference/system.webserver/staticcontent/mimemap

They both feature this stanza from the web.config file.

<configuration>
  <system.webServer>
    <staticContent>
      <remove fileExtension=".extension" />
      <mimeMap fileExtension=".extension" mimeType="application/example" />
    </staticContent>
  </system.webServer>
</configuration>

If that's how to actually fix the 406.0 problem...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM