Returning a custom response code for for serving static html when using IIS ApplicationInitialization remapManagedRequestsTo feature?
Asked Answered
F

1

6

I'm currently using the ApplicationInitialization feature of IIS to warm up my ASP.NET application. I've set the attribute remapManagedRequestsTo to "warmup.html".

<applicationInitialization remapManagedRequestsTo="warmup.html" skipManagedModules="true" doAppInitAfterRestart="true" >
  <add initializationPage="/home" />  
  <add initializationPage="/about-us" />      
</applicationInitialization>

It's working well but I would like to return a custom status code when the content for Warmup.html is returned to the browser. This is so that when I run some smoke tests after deployment I get to know when the warm up has finished.

I've tried using URL Rewrite to change the status code from 200 to 555 to serve up warmup.html and it does change the status code but doesn't serve the content in warmup.html

<rewrite>
  <rules>
    <rule name="Change warm up status code" stopProcessing="true">
      <match url="warmup.html" />          
      <action type="CustomResponse" statusCode="555" subStatusCode="0"/>        
  </rule>
</rules>
</rewrite>

Is there a way I can do both the serving of warmup.html's content AND return a custom status code of 555?

Fullgrown answered 22/11, 2017 at 14:33 Comment(0)
F
9

Finally found my answer in a blog post written by Morten Bock

Turns out I have to remove the two attributes remapManagedRequestsTo and skipManagedModules (default value of false) which leaves us with

<applicationInitialization doAppInitAfterRestart="true">
  <add initializationPage="/home" />  
  <add initializationPage="/about-us" />      
</applicationInitialization>

Then let the URL Rewrite module take over but we want to rewrite the response code when the Application Initialization is making the request marked by the server variable APP_WARMING_UP containing a value of 1. When this condition is met we can create a custom response as the action and pop the statusCode attribute with 555.

<rewrite>
    <rules>
        <rule name="WarmUp" patternSyntax="Wildcard" stopProcessing="true">
            <match url="*" />
            <conditions>
              <add input="{APP_WARMING_UP}" pattern="1" />
            </conditions>
            <action type="CustomResponse" statusCode="555" statusReason="Site is warming up" statusDescription="Try again shortly" />
        </rule>
    </rules>
</rewrite>

Then to catch the status 555 as a custom error and direct the user to the friendly warm up page warmup.html

<system.webServer>
    <httpErrors errorMode="Custom">
        <error statusCode="555" path="warmup.html" responseMode="File" />
    </httpErrors>
</system.webServer>
Fullgrown answered 27/2, 2018 at 3:54 Comment(1)
This answer is really helpful. Thanks!Beaton

© 2022 - 2024 — McMap. All rights reserved.