最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

HTTP 404 error with Swagger UI on Azure Functions app - Stack Overflow

programmeradmin5浏览0评论

My .NET 8 Azure Functions app has the following dependencies:

<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="MediatR" Version="12.4.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.20.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.OpenApi" Version="1.5.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.4" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />

When I run it in the VS2022 IDE, I am able to see the Swagger UI at http://localhost:7230/api/swagger/ui

When I run the deployed app I can see the blue screen stating "Your Functions 4.0 appl is up and running".

How do I get to the Swagger UI?

If I just try adding /api/swagger/ui to the URL, I get a "404 Not Found" error.

I was expecting this to work the same way when I added /api/swagger/ui to the as localhost url

I have looked in application insights but didn't see any failed requests.

Update

In local.settings.json I have

"ASPNETCORE_ENVIRONMENT": "dev"

then in Program.cs I use this environment variable to work out which appsettings.json to use

update

My problem was that I had not set up the ASPNETCORE_ENVIRONMENT in Azure.

My .NET 8 Azure Functions app has the following dependencies:

<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="MediatR" Version="12.4.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.20.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.OpenApi" Version="1.5.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.4" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />

When I run it in the VS2022 IDE, I am able to see the Swagger UI at http://localhost:7230/api/swagger/ui

When I run the deployed app I can see the blue screen stating "Your Functions 4.0 appl is up and running".

How do I get to the Swagger UI?

If I just try adding /api/swagger/ui to the URL, I get a "404 Not Found" error.

I was expecting this to work the same way when I added /api/swagger/ui to the as localhost url

I have looked in application insights but didn't see any failed requests.

Update

In local.settings.json I have

"ASPNETCORE_ENVIRONMENT": "dev"

then in Program.cs I use this environment variable to work out which appsettings.json to use

update

My problem was that I had not set up the ASPNETCORE_ENVIRONMENT in Azure.

Share Improve this question edited Jan 22 at 10:54 RithwikBojja 11.4k2 gold badges5 silver badges15 bronze badges Recognized by Microsoft Azure Collective asked Jan 18 at 4:06 KirstenKirsten 18.2k50 gold badges208 silver badges361 bronze badges 5
  • 1 Have you enabled swagger for both development and production? Can you share how you've enabled it? – Jeppe Commented Jan 18 at 7:12
  • Thank you you have the answer. I updated the question and tried setting the environment variable in Azure and can see the swagger ui. Care to write it up? – Kirsten Commented Jan 19 at 5:57
  • 1 I'm not sure what to conclude, since I don't have all information. But have a look at this guide - it shows accessing swagger from a deployed function: github/Azure/azure-functions-openapi-extension/blob/… – Jeppe Commented Jan 19 at 7:15
  • 1 Can you try to add environment variable "ASPNETCORE_ENVIRONMENT" is set to "development" in portal. – Pavan Commented Jan 20 at 3:22
  • Thank you Pavan and Jessie. My problem was that I needed to set the environment variable in Azure – Kirsten Commented Jan 21 at 4:47
Add a comment  | 

1 Answer 1

Reset to default 0

I do agree with @Jeppe and @Pavan, it did work for me using below code:

function.cs:

using System.IO;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Enums;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json;

namespace RithApp
{
    public class Function1
    {
        private readonly ILogger<Function1> ri_lg;

        public Function1(ILogger<Function1> lg)
        {
            ri_lg = lg;
        }

        [FunctionName("Function1")]
        [OpenApiOperation(operationId: "Run", tags: new[] { "name" })]
        [OpenApiSecurity("function_key", SecuritySchemeType.ApiKey, Name = "code", In = OpenApiSecurityLocationType.Query)]
        [OpenApiParameter(name: "name", In = ParameterLocation.Query, Required = false, Type = typeof(string), Description = "The **Name** parameter")]
        [OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "text/plain", bodyType: typeof(string), Description = "The OK response")]
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req)
        {
            ri_lg.LogInformation("Hello Rithwik, function started");

            string rithmsg = $"Hello, Rithwik . This HTTP triggered function executed successfully.";

            return new OkObjectResult(rithmsg);
        }
    }
}

local.settings.json:

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_INPROC_NET8_ENABLED": "1",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
}

csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.OpenApi" Version="1.5.1" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.5.0" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

Deployed to Azure:

Output:

Checked the swagger using below api:

https://funcappname.azurewebsites/api/swagger/ui

发布评论

评论列表(0)

  1. 暂无评论