0

I have a .NET 6.0 isolated function and I have added OpenAPI support to it. This is a snippet from my Program.cs:

var host = new HostBuilder()
                   .ConfigureFunctionsWorkerDefaults(worker => worker.UseNewtonsoftJson())
                   .ConfigureServices(services =>
                       {
                           services.AddSingleton<IOpenApiConfigurationOptions>(_ =>
                               {
                                   var options = new OpenApiConfigurationOptions()
                                       {
                                           Info = new OpenApiInfo()
                                                  {
                                                      Version = "1.0.0",
                                                      Title = "Function Name",
                                                      Description = "This is the name of the function",
                                                      Contact = new OpenApiContact()
                                                                {
                                                                    Name = "Codebug"
                                                                }                    
                                                  },
                                           Servers = DefaultOpenApiConfigurationOptions.GetHostNames(),
                                           OpenApiVersion = OpenApiVersionType.V2,
                                           IncludeRequestingHostName = true,
                                           ForceHttps = false,
                                           ForceHttp = false,
                                       };

                                  return options;
                              });
                        })    
                   .Build();

host.Run();

This is working without any issue. When I run the function in the console, I get presented with the following urls:

FunctionName: [POST] http://localhost:7295/api/FunctionName

RenderOAuth2Redirect: [GET] http://localhost:7295/api/oauth2-redirect.html

RenderOpenApiDocument: [GET] http://localhost:7295/api/openapi/{version}.{extension}

RenderSwaggerDocument: [GET] http://localhost:7295/api/swagger.{extension}

RenderSwaggerUI: [GET] http://localhost:7295/api/swagger/ui

My function's url starts with <base url>/api/FunctionName, however all the OpenAPI specific urls are in the root of the api route, like <base url>/api/swagger/ui.

How can I change the OpenAPI default url so that the the urls do not appear under /api route, but have a following format

<base url>/api/FunctionName/swagger/ui

While investigating I have come up with this Stack Overflow Answer. However it shows how to add the Swagger defaults to the app object. I do not know how to get hold of an app object in an isolated function where can I add the swagger default values?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
imran chowdhury
  • 331
  • 1
  • 5
  • 18
  • Try adding [this](https://i.imgur.com/5eRKpec.png) to your host.json file and then your URLs will look like [this](https://i.imgur.com/tVefQP4.png) – Ikhtesam Afrin Aug 25 '23 at 12:33

1 Answers1

0

How can I change the OpenAPI default url so that the the urls do not appear under /api route

You can add routePrefix in your host.json file

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      },
      "enableLiveMetricsFilters": true
    }
  },
  "extensions": {
    "http": {
      "routePrefix": "api/{Your function name}"
    }
  }
}

Output:

enter image description here

BasePath is also changed in .json file

enter image description here

Ikhtesam Afrin
  • 897
  • 1
  • 1
  • 6