启动我的应用程序时无法访问swagger UI



我使用的是ASP.NET核心6,我使用react选项创建了我的应用程序(所以我的文件夹中有一个ClientApp)。

当我运行应用程序时,我会看到我的react UI,而无法看到swagger UI。

通常,当你选择从两个不同的文件夹构建frontendbackend时,如果你在visualstudio上运行后端,你会看到swagger UI。

但现在我只看到我的react应用程序界面。我正试图用https://localhost:44434/swaggerhttps://localhost:44434/swagger/index.html访问swagger,但它只是停留在我的react应用程序UI页面上。

这是我试图设置的视频:

https://www.youtube.com/watch?v=RvwvFmO-56E

知道吗?非常感谢!

这是我的program.cs代码:

using BugTracker.Context;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
//app.UseHsts();
}
app.UseSwagger();
app.UseSwaggerUI();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();

app.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
app.MapFallbackToFile("index.html"); ;
app.Run();

这是我的控制器代码:

using Microsoft.AspNetCore.Mvc;
using System;
namespace BugTracker.Controllers
{
[ApiController]
[Route("api/authentication")]
public class AuthController : Controller
{
[HttpGet]
public IActionResult Test()
{
return Ok("success");
}
}
}

除了添加EndpointsApiExplorer和SwaggerGen服务外,您还必须修改客户端应用程序中的setupProxy.js文件并添加swagger端点。

你的Program.cs文件应该是这样的:

var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();

app.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
app.MapFallbackToFile("index.html");
app.Run();

您的useProxy.js文件应该如下所示:

const { createProxyMiddleware } = require('http-proxy-middleware');
const { env } = require('process');
const target = env.ASPNETCORE_HTTPS_PORT ? `https://localhost:${env.ASPNETCORE_HTTPS_PORT}` :
env.ASPNETCORE_URLS ? env.ASPNETCORE_URLS.split(';')[0] : 'http://localhost:37316';
const context =  [
"/swagger",
"/weatherforecast",
];
module.exports = function(app) {
const appProxy = createProxyMiddleware(context, {
target: target,
secure: false,
headers: {
Connection: 'Keep-Alive'
}
});
app.use(appProxy);
};

如果您正在创建WebAPI+React UI:

  1. Program.cs文件中,您应该添加builder.Services.AddControllers();

  2. 您的AuthController类应该继承ControllerBaseCCD_ 10。

  3. app.UseRouting();之后尝试使用app.UseEndpoints(endpoint => endpoint.MapControllers());

我遇到了完全相同的问题。什么都不起作用(使用NET6的Angular SPA)。真见鬼,我把说明书上说要使用的2行注释掉了,它就开始工作了!

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

最新更新