How remove "api" from route - Azure Functions v3 -- host.json routePrefix empty string doesn't work

Viewed 637

I'm trying to remove the "api" prefix in my URL.

This is my host.json.

{
  "version": "2.0",
  "extensions":   {
    "http":     {
      "routePrefix": "" 
    }
  }
}

NOTE: if I change "routePrefix" : "foo", it works. It seems that the string.empty is causing the problem.

This is my startup.cs code:

public class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {

        var serviceProvider = builder.Services.BuildServiceProvider();
        var existingConfig = serviceProvider.GetService<IConfiguration>(); 

        var config = new ConfigurationBuilder()
            .AddEnvironmentVariables()
            .AddConfiguration(existingConfig) 
            .Build();

        builder.Services.AddSingleton<IConfiguration>(configRoot);
        services.BuildServiceProvider(true);

    }
}

Note that I am already adding the existing configuration to the builder, as per documentation in here:

https://github.com/Azure/azure-functions-host/issues/4363

https://github.com/Azure/azure-functions-durable-extension/issues/894

And this issue, someone else also reported the same problem as me. But the ticket is closed...

https://github.com/Azure/azure-functions-host/issues/5466

What else am I missing or doing wrong?

aside: if I comment out this line, then the URL with api also works. builder.Services.AddSingleton(configRoot);

1 Answers

My problem is this line:

builder.Services.AddSingleton<IConfiguration>(configRoot);

We had this line of code in our project because we were passing in IConfiguration directly to our classes. However, we didn't actually need it, as we are now using IOptions for passing in our typed configuration settings.

FYI: These are the nuget packages

enter image description here

Related