Custom HTTP status code for a specific resource in IIS

I have a website with one file 'app_offline.htm'. How to configure IIS to return it with a specific status code (say, 209), and not by default (200)?

+4
source share
3 answers

An alternative to using the app_offline.htm ASP.Net function is to use the IIS URL Rewrite Module , it has a huge package 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 this module you can configure a rule that will return an empty response with code 209 to any request, if this rule is enabled.

This will be implemented in your web.config somehow 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> 
+4
source

Since IIS has special processing for a file named 'app_offline.htm' in the root directory of your website, this sentence may not work if you do not rename the file. But, here, anyway ...

You can use the 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 replace the correct information in the type attribute.

In HttpHandler do something like this:

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

To show specific content based on the status code, you can let the response set the status code and display the content displayed based on the code in the httpErrors section. An example in this post is https://serverfault.com/questions/483145/how-to-add-a-site-wide-downtime-error-message-in-iis-with-a-custom-503-error-co

In order to save, I will reproduce 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> 
+1
source

Source: https://habr.com/ru/post/1380380/


All Articles