IIS 中特定资源的自定义 http 状态代码
我有一个带有单个“app_offline.htm”文件的网站。我如何配置 IIS 以使用特定状态代码(例如 209)而不是默认状态代码(200)返回它?
I have a web site with a single 'app_offline.htm' file. How can i configure IIS to return it with specific status code (say, 209) instead of default one (200)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 ASP.Net 的“app_offline.htm”功能的替代方法是使用 IIS URL重写模块,它有一个一大堆技巧和设置自定义响应代码就是其中之一。
如果页面的内容不重要,只重要响应代码,那么使用该模块,您可以配置一个规则,只要该规则满足以下条件,该规则将向任何请求返回带有 209 代码的空白响应已启用。
这将在您的 web.config 中具体化,如下所示:
An alternative to using the 'app_offline.htm' feature of ASP.Net could be to use the IIS URL Rewrite Module, it has a huge bag of tricks and setting custom response codes is one of them.
If the content of the page is not important, only the response code, then with that module you can configure a rule that will return a blank response with a 209 code to any request as long as the rule is enabled.
That will materialise in your web.config as something like this:
由于 IIS 对网站根目录中名为“app_offline.htm”的文件进行了特殊处理,因此此建议可能不起作用,除非您重命名该文件。但是,无论如何……
您可以为此使用 HttpHandler。在您的 web.config 中,添加如下内容:
显然,请替换“type”属性中的正确信息。
在 HttpHandler 中,执行如下操作:
Since IIS has special handling for a file named 'app_offline.htm' in the root of your web site, this suggestion may not work, unless you rename the file. But, here it is anyway...
You can use an HttpHandler for this. In your web.config, add something like this:
Obviously, substitute the proper info in the 'type' attribute.
In the HttpHandler, do something like this:
要根据状态代码显示特定内容,您可以让响应设置状态代码并根据 httpErrors 部分中的代码映射要显示的内容。请参阅这篇文章中的示例 https://serverfault.com/questions/483145/how-to-add-a-site-wide-downtime-error-message-in-iis-with-a-custom-503-error-co
为了保存的目的,我将复制下面的例子:
To show specific content based on status code, you can let the response set the status code and map the content to show based on the code in the httpErrors section. See example in this post https://serverfault.com/questions/483145/how-to-add-a-site-wide-downtime-error-message-in-iis-with-a-custom-503-error-co
For the benefit of preservation, I will replicate the example below: