0

I have an ASP.NET MVC application running as an Azure App Service. There's a deprecated API included on mydomain.com/api/* that I want to close down because I now have api.mydomain.com/*. Once the deprecated API is shut down, some people may still be making requests to it. I can create a simple controller that returns 404 or something like that. But I would like to hear if someone knows a solution in Azure where I can "blacklist" URLs like /api/* or similar before even hitting my application. I know that there are different products available on Azure where I can include handlers before my website, like API Management. But I would prefer not to change too much, why a feature directly on the app service, a rewrite rule, or similar would be awesome here. I simply don't want the performance overhead of invoking my own application when a request for a URL that I know shouldn't do anything comes in.

ThomasArdal
  • 4,999
  • 4
  • 33
  • 73

1 Answers1

0

You can achieve that using URL Rewrite. Rather than blocking the old path, you just redirect to the new one. Add the following block to a web.config file

<configuration>
  <system.webServer>  
    <rewrite>  
        <rules>  
          <rule name="Redirect rquests to default azure websites domain" stopProcessing="true">
            <match url="(.*)" />  
            <conditions logicalGrouping="MatchAny">
              <add input="{HTTP_HOST}" pattern="^mydomain\.com\/api/(.*)" />
            </conditions>
            <action type="Redirect" url="https://api.mydomain.com/{R:0}" />  
          </rule>  
        </rules>  
    </rewrite>  
  </system.webServer>  
</configuration>

Source: https://stackoverflow.com/a/36631234/1384539

Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90
  • I don't want to redirect to the new API since the interface is different. But I guess I can redirect to a 404 response or something like that? – ThomasArdal Dec 20 '19 at 17:49
  • yes, you can abort the request or use a custom response with the desired status code: https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference – Thiago Custodio Dec 20 '19 at 17:57