IIS 中特定资源的自定义 http 状态代码

发布于 2024-12-14 08:51:00 字数 83 浏览 1 评论 0原文

我有一个带有单个“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

辞别 2024-12-21 08:51:00

使用 ASP.Net 的“app_offline.htm”功能的替代方法是使用 IIS URL重写模块,它有一个一大堆技巧和设置自定义响应代码就是其中之一。

如果页面的内容不重要,只重要响应代码,那么使用该模块,您可以配置一个规则,只要该规则满足以下条件,该规则将向任何请求返回带有 209 代码的空白响应已启用。

这将在您的 web.config 中具体化,如下所示:

<configuration>
    <system.webServer>       
        <rewrite>
            <rules>
                <rule name="app_offline" stopProcessing="true">
                    <match url=".*" />
                    <action type="CustomResponse" statusCode="209" statusReason="System unavailable" statusDescription="The site is currently down for maintenance, or something." />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

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:

<configuration>
    <system.webServer>       
        <rewrite>
            <rules>
                <rule name="app_offline" stopProcessing="true">
                    <match url=".*" />
                    <action type="CustomResponse" statusCode="209" statusReason="System unavailable" statusDescription="The site is currently down for maintenance, or something." />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>
少钕鈤記 2024-12-21 08:51:00

由于 IIS 对网站根目录中名为“app_offline.htm”的文件进行了特殊处理,因此此建议可能不起作用,除非您重命名该文件。但是,无论如何……

您可以为此使用 HttpHandler。在您的 web.config 中,添加如下内容:

<add name="app_offline_GET" path="app_offline.htm" verb="GET" type="namespace.classname,dllname" />

显然,请替换“type”属性中的正确信息。

在 HttpHandler 中,执行如下操作:

public void ProcessRequest(HttpContext context) {
  context.Response.WriteFile("app_offline.htm");
  context.Response.StatusCode = 209;        
  context.Response.StatusDescription = "whatever";
}

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:

<add name="app_offline_GET" path="app_offline.htm" verb="GET" type="namespace.classname,dllname" />

Obviously, substitute the proper info in the 'type' attribute.

In the HttpHandler, do something like this:

public void ProcessRequest(HttpContext context) {
  context.Response.WriteFile("app_offline.htm");
  context.Response.StatusCode = 209;        
  context.Response.StatusDescription = "whatever";
}
少年亿悲伤 2024-12-21 08:51:00

要根据状态代码显示特定内容,您可以让响应设置状态代码并根据 httpErrors 部分中的代码映射要显示的内容。请参阅这篇文章中的示例 https://serverfault.com/questions/483145/how-to-add-a-site-wide-downtime-error-message-in-iis-with-a-custom-503-error-co

为了保存的目的,我将复制下面的例子:

<system.webServer>
    ...
    <rewrite>
        <rules>
            <rule name="SiteDown" stopProcessing="true">
                <match url=".*" />
                <action type="CustomResponse" statusCode="503" statusReason="Down for maintenance" statusDescription="will be back up soon" />
            </rule>
        </rules>
    </rewrite>

      <httpErrors existingResponse="Auto" errorMode="Custom" defaultResponseMode="File">
             <remove statusCode="503" subStatusCode="-1" />
             <error statusCode="503" path="503.html" />
      </httpErrors>
</system.webServer>

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:

<system.webServer>
    ...
    <rewrite>
        <rules>
            <rule name="SiteDown" stopProcessing="true">
                <match url=".*" />
                <action type="CustomResponse" statusCode="503" statusReason="Down for maintenance" statusDescription="will be back up soon" />
            </rule>
        </rules>
    </rewrite>

      <httpErrors existingResponse="Auto" errorMode="Custom" defaultResponseMode="File">
             <remove statusCode="503" subStatusCode="-1" />
             <error statusCode="503" path="503.html" />
      </httpErrors>
</system.webServer>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文