[英]Custom http status code for specific resource in IIS
我有一個網站,上面有一個'app_offline.htm'文件。 我如何配置IIS以使用特定的狀態代碼(例如,209)而不是默認的(200)來返回它?
使用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>
由於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";
}
要根據狀態代碼顯示特定內容,您可以讓響應設置狀態代碼,並根據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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.