Azure Functions v4 - Provide a Function at the content root url


Posted on 8/10/2022


Azure Functions provided in the last 3 runtime versions the option to redirect/proxy certain requests. This option was removed with the v4 version. Microsoft recommends to use Azure API Management instead. While this works for most cases where you want to proxy another API. This does not help however, if you need to provide a function at the content root of your function (e.g. https://myfunctionhost.azurewebsites.net/)

Until now you were able to create a proxies.json to redirect the root url to the function route.

{
  "Home": {
    "matchCondition": {
      "methods": ["GET"],
      "route": "/"
    },
    "backendUri": "https://localhost:7071/api/home"
  }
}

With Azure Functions v4 this is gone now. But luckily there is another way. In the host.json file we can configure the http extenstion to get rid of the /api prefix.

Adding "extensions": { "http": { "routePrefix": "" } } here will remove the route prefix.

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensions": { "http": { "routePrefix": "" } }
}

In the app setting configuration (local setting file and azure function app settings) we need to disable the default function homepage by adding "AzureWebJobsDisableHomepage ": true

At last we need to set the route parameter in the function code to be "/"

[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "/")] HttpRequest req,

When you start the Function now locally you will see the following: Azure function console output showing http://localhost:7071//

If you open this url directly, you will get an http not found exception which is technically correct as we serve the function not at this url.

It seems to be a display bug. Removing the second / from the url will however, trigger the correct function.

HTTP 404 not found exception