Home > Software design >  Running .NET6 SwaggerApi on a raspberrypi
Running .NET6 SwaggerApi on a raspberrypi

Time:06-28

i am trying to run a .Net6 Entity Framework API on my raspberrypi.

After following the Microsoft Docs and running the application, i cannot access swaggerui or API.

There are no errors poping up in console when i try to access the page via http://localhost:5000/

The webbrowser just returns a 401 Error.

Following Code:

Program.cs:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<programContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("Context") ?? throw new InvalidOperationException("Connection string 'Context' not found.")));


builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();


var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseHttpsRedirection();
}
app.UseSwagger();
app.UseSwaggerUI();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();

app.Run();

appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "programApiContext": "Server=(localdb)\\mssqllocaldb;Database=programApi.Data;Trusted_Connection=True;MultipleActiveResultSets=true",
    "programContext": "Server=(localdb)\\mssqllocaldb;Database=programApi.Data;Trusted_Connection=True;MultipleActiveResultSets=true"
  }

dotnet -info:

.NET SDK (gemäß "global.json"):
 Version:   6.0.301
 Commit:    43f9b18481

Laufzeitumgebung:
 OS Name:     debian
 OS Version:  11
 OS Platform: Linux
 RID:         debian.11-arm64
 Base Path:   /opt/dotnet/sdk/6.0.301/

Host (useful for support):
  Version: 6.0.6
  Commit:  7cca709db2

.NET SDKs installed:
  6.0.301 [/opt/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.6 [/opt/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.6 [/opt/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download

nginx-site config:

server {
    listen        80;
    location / {
        proxy_pass         http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }
}

CodePudding user response:

To retrieve the Swagger document you need to use its full path. From the Get started with Swashbuckle and ASP.NET Core docs:

Launch the app and navigate to https://localhost:/swagger/v1/swagger.json. The generated document describing the endpoints appears as shown in OpenAPI specification (openapi.json).

The Swagger UI can be found at https://localhost:/swagger. Explore the API via Swagger UI and incorporate it in other programs.

You have to use extra code to display the UI at the root

To serve the Swagger UI at the app's root (https://localhost:/), set the RoutePrefix property to an empty string:

app.UseSwaggerUI(options =>
{
    options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
    options.RoutePrefix = string.Empty;
});

CodePudding user response:

After launch your application follow bellow link to get swagger

https://localhost:5000/swagger/index.html

Check your launchSettings.json

    "development": {
    "commandName": "Project",
    "dotnetRunMessages": true,
    "launchBrowser": true,
    "launchUrl": "swagger",
    "applicationUrl": "https://localhost:5000",
    "environmentVariables": {
      "ASPNETCORE_ENVIRONMENT": "development"
    }
  }
  • Related