我一直在玩blazor wasm,在默认项目创建的服务器项目上使用swagger时遇到了问题。这个问题只发生在Chrome中,而不是Edge。
问题很奇怪,我设置了招摇,当我去https://localhost:44323/swagger/index.html我有一个工作的swagger页面,但当我尝试使用我的任何控制器,甚至默认的天气控制器时,它只会运行并坐在那里说永远加载。如果我把断点放在控制器上,它就会被击中。如果我打开浏览器调试工具,并停止它,它会说"在调试器中暂停";浏览器将闪烁,然后显示结果。
如果我去https://localhost:44323/WeatherForecast它运行并给出正确的响应。
我在Visual studio中添加了该项目,正在进行新项目=>选择blazor app=>Blazor WebAssembly应用程序,并选择AspNetCore托管,以及Progressive Web应用程序。
我已通过nuget安装旋转皮带扣。AspNetCore。SwaggerGen v.5.0
旋转皮带扣。AspNetCore。SwaggerUI v.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");
});
}
}
我遇到了类似的问题,但我能够通过在services.AddControllersWithViews();
和services.AddRazorPages();
之后添加services.AddControllers();
来提出它。