This provides sample codes to show Swagger UI on root (/), instead of /api/swagger/ui.
There are two function apps available – one for the in-proc worker and the other for the out-of-proc worker. Take a look at your preferred function app depending on your situation.
To use the root page as Swagger UI, follow the steps below. Note that there's no way to use HTTP URL rewrite feature as of today.
Update your host.json like below:
Add the following app settings key:
{ "IsEncrypted": false, "Values": { ... // Disable the homepage "AzureWebJobsDisableHomepage": true, ... } }Add a new HTTP trigger for URL redirection.
// In-Proc function public class RedirectHttpTrigger { // Add this HTTP trigger for URL redirection [FunctionName(nameof(RedirectHttpTrigger.Redirect))] [OpenApiIgnore()] public async Task<IActionResult> Redirect( // Make sure the route MUST be "/" [HttpTrigger(AuthorizationLevel.Anonymous, "GET", Route = "/")] HttpRequest req) { this._logger.LogInformation("C# HTTP trigger function processed a request."); // Make sure the redirection URL MUST be "/swagger/ui" var result = new RedirectResult("/swagger/ui", permanent: true); return await Task.FromResult(result).ConfigureAwait(false); } }// Out-of-Proc function public class RedirectHttpTrigger { // Add this HTTP trigger for URL redirection [Function(nameof(RedirectHttpTrigger.Redirect))] [OpenApiIgnore()] public HttpResponseData Redirect( // Make sure the route MUST be "/" [HttpTrigger(AuthorizationLevel.Anonymous, "GET", Route = "/")] HttpRequestData req) { _logger.LogInformation("C# HTTP trigger function processed a request."); // Make sure to put the HTTP status code of 301 (move permanently) var response = req.CreateResponse(HttpStatusCode.MovedPermanently); // Make sure to put the "Location" header with the redirection URL of "/swagger/ui" response.Headers.Add("Location", "/swagger/ui"); return response; } }If you want to run this sample app on your GitHub Codespaces, please run the script first:
# On bash shell pwsh -c "Invoke-RestMethod https://aka.ms/azfunc-openapi/add-codespaces.ps1 | Invoke-Expression"# On PowerShell Invoke-RestMethod https://aka.ms/azfunc-openapi/add-codespaces.ps1 | Invoke-ExpressionThis script will update your local.settings.json file to accommodate your GitHub Codespaces to work as a local running environment.
To deploy both Azure Functions app, you can use Azure Developer CLI by running those two commands:
# Initialise environment azd init # Provision and deploy apps azd up
{ "version": "2.0", ... "extensions": { "http": { // Remove the default route prefix from 'api' to '' "routePrefix": "" } } }