【发布时间】:2021-09-19 09:43:09
【问题描述】:
我一直在玩 blazor wasm,但在默认项目创建的服务器项目上使用 swagger 时遇到了问题。这个问题只发生在 Chrome 中,而不是 Edge。
问题很奇怪,我已经设置了 swagger,当我转到 https://localhost:44323/swagger/index.html 时,我得到了一个有效的 swagger 页面,但是当我尝试使用我的任何控制器或即使是默认天气,它也只是运行并坐在那里说永远加载。如果我在控制器中放置断点,它确实会被击中。 如果我打开浏览器调试工具并停止它,它会说“在调试器中暂停”,浏览器会闪烁然后显示结果。
如果我访问 https://localhost:44323/WeatherForecast,它会运行并给出正确的响应。
我在 Visual Studio 中添加了项目,进入新项目 => 选择 blazor 应用程序 => Blazor WebAssembly 应用程序,然后选择 AspNetCore 托管和渐进式 Web 应用程序。
我已经通过 nuget 安装了 Swashbuckle.AspNetCore.SwaggerGen v5.5.0
Swashbuckle.AspNetCore.SwaggerUI v5.5.0
我的整个创业课程是
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddRazorPages();
//Added Swagger
services.AddSwaggerGen(setUpAction =>
{
setUpAction.SwaggerDoc("PetStoreAPI", new OpenApiInfo { Title = "PetStore API", Version = "1" });
//Add comments, to get this to work you need to go into project properties, build tab, then select "XML Documentation file"
var xmlCommentFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlCommentFullPath = Path.Combine(AppContext.BaseDirectory, xmlCommentFile);
setUpAction.IncludeXmlComments(xmlCommentFullPath);
});
}
public void ConfigureContainer(ContainerBuilder builder)
{
// Add any Autofac modules or registrations.
// This is called AFTER ConfigureServices so things you
// register here OVERRIDE things registered in ConfigureServices.
//
// You must have the call to `UseServiceProviderFactory(new AutofacServiceProviderFactory())`
// when building the host or this won't be called.
builder.RegisterModule(new Autofac.AutofacConfiguration());
builder.AddAutoMapper(typeof(Startup).Assembly);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseWebAssemblyDebugging();
}
else
{
app.UseExceptionHandler("/Error");
// 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();
//Added Swagger
app.UseSwagger();
app.UseSwaggerUI(setupAction =>
{
setupAction.SwaggerEndpoint("/swagger/PetStoreAPI/swagger.json", "PetStore API");
});
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
endpoints.MapFallbackToFile("index.html");
});
}
}
【问题讨论】:
标签: google-chrome swagger blazor swashbuckle