Returning a custom response code for for serving s

2019-05-14 14:54发布

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?

1条回答
贼婆χ
2楼-- · 2019-05-14 15:15

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>
查看更多
登录 后发表回答