簡體   English   中英

在樹莓派上運行 .NET6 SwaggerApi

[英]Running .NET6 SwaggerApi on a raspberrypi

我正在嘗試在我的樹莓派上運行 a.Net6 實體框架 API。

按照 Microsoft 文檔並運行應用程序后,我無法訪問 swaggerui 或 API。

當我嘗試通過 http://localhost:5000/ 訪問頁面時,控制台中沒有彈出錯誤

網絡瀏覽器只返回 401 錯誤。

以下代碼:

程序.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();

應用設置.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"
  }

do.net-信息:

.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 站點配置:

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;
    }
}

啟動您的應用程序后,請點擊以下鏈接獲取 swagger

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

檢查你的launchSettings.json

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

要檢索 Swagger 文檔,您需要使用其完整路徑。 開始使用 Swashbuckle 和 ASP.NET 核心文檔:

啟動應用程序並導航到https://localhost:/swagger/v1/swagger.json 生成的描述端點的文檔如 OpenAPI 規范 (openapi.json) 中所示。

Swagger UI 可以在https://localhost:/swagger找到。 通過 Swagger UI 探索 API 並將其合並到其他程序中。

您必須使用額外的代碼在根目錄下顯示 UI

要在應用程序的根目錄 (https://localhost:/) 提供 Swagger UI,請將 RoutePrefix 屬性設置為空字符串:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM