简体   繁体   English

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

[英]Custom http status code for specific resource in IIS

I have a web site with a single 'app_offline.htm' file. 我有一个网站,上面有一个'app_offline.htm'文件。 How can i configure IIS to return it with specific status code (say, 209) instead of default one (200)? 我如何配置IIS以使用特定的状态代码(例如,209)而不是默认的(200)来返回它?

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. 使用ASP.Net的'app_offline.htm'功能的另一种方法可能是使用IIS URL重写模块 ,它有一大堆技巧 ,设置自定义响应代码就是其中之一。

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. 如果页面内容不重要,只有响应代码,那么使用该模块,您可以配置一个规则,只要规则启用,该规则将向任何请求返回带有209代码的空白响应。

That will materialise in your web.config as something like this: 这将在你的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>

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. 由于IIS对您网站根目录中名为“app_offline.htm”的文件进行了特殊处理,因此除非您重命名该文件,否则此建议可能无效。 But, here it is anyway... 但是,无论如何......

You can use an HttpHandler for this. 你可以使用HttpHandler。 In your web.config, add something like this: 在您的web.config中,添加如下内容:

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

Obviously, substitute the proper info in the 'type' attribute. 显然,用'type'属性替换正确的信息。

In the HttpHandler, do something like this: 在HttpHandler中,执行以下操作:

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

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. 要根据状态代码显示特定内容,您可以让响应设置状态代码,并根据httpErrors部分中的代码映射要显示的内容。 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 请参阅此文中的示例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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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