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

r1zk6ea1  于 2022-11-06  发布在  其他
关注(0)|答案(3)|浏览(230)

我正在使用ASP.NETCore6,我创建了带有react选项的应用程序(所以我的文件夹中有一个ClientApp)。
当我运行应用程序时,我会看到我的react用户界面,而无法看到swagger用户界面。
通常,当你选择从两个不同的文件夹构建frontendbackend时,如果你在visual studio上运行后端,你会看到swagger UI。
但现在我只看到我的React应用UI。我试图用X1 M2 N1 X或X1 M3 N1 X访问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");
        }
    }
}
ni65a41a

ni65a41a1#

除了添加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);
};
wz1wpwve

wz1wpwve2#

如果要创建WebAPI + React UI:
1.在Program.cs文件中,应添加builder.Services.AddControllers();
1.您的AuthController类别应该继承ControllerBase,而不是Controller
1.在app.UseRouting();之后尝试使用app.UseEndpoints(endpoint => endpoint.MapControllers());

kzipqqlq

kzipqqlq3#

我有完全相同的问题。没有任何工作(Angular SPA与NET 6)。为见鬼的是,我注解了2行的指示说要使用,它开始工作!

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

相关问题