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

此生再无相见时 提交于 2019-12-06 05:57:57

问题


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:


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>


来源:https://stackoverflow.com/questions/47437243/returning-a-custom-response-code-for-for-serving-static-html-when-using-iis-appl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!