How to add rewrite rule to Azure web.config so that certain requests are not redirected?

冷暖自知 提交于 2021-02-10 16:19:50

问题


How to add a URL Rewrite rule so that requests from user agents – ReadyForRequest/1.0+(HealthCheck) & HealthCheck/1.0 are not redirected ? I need this so Azure app service health check can work.

I found many SO posts about redirecting urls but I can't found SO posts for preventing redirection for certain requests.

This post is to add answer to my previous post, but I only want to focus on the real problem because my previous post seems gained attention to the wrong topic.

Thanks for any help


回答1:


You can add web.config in your project. It work for me.

Method 2

In Startup.cs.

public void Configure(IApplicationBuilder app)
{
    app.UseRouting();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapHealthChecks("/health");

        endpoints.MapGet("/{**path}", async context =>
        {
            await context.Response.WriteAsync(
                "Navigate to /health to see the health status.");
        });
    });
}

In web.config.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <urlCompression doDynamicCompression="true" doStaticCompression="true"/>
    <httpCompression>
      <staticTypes>
        <add mimeType="image/svg+xml" enabled="true"/>
      </staticTypes>
    </httpCompression>
    <rewrite>
        <rules>
            <rule name="Root Hit Redirect" stopProcessing="true">
                <match url="^$" />
                <action type="Redirect" url="/health" />
            </rule>
        </rules>
    </rewrite>
  </system.webServer>
</configuration>


来源:https://stackoverflow.com/questions/65639409/how-to-add-rewrite-rule-to-azure-web-config-so-that-certain-requests-are-not-red

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