首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Chrome中使用swagger和Blazor WebAssembly

在Chrome中使用swagger和Blazor WebAssembly
EN

Stack Overflow用户
提问于 2020-06-24 01:50:07
回答 1查看 1K关注 0票数 0

我一直在玩blazor wasm,在默认项目创建的服务器项目上使用swagger遇到了一个问题。这个问题只在Chrome中发生,而不是Edge。

问题很奇怪,我设置了swagger,当我去https://localhost:44323/swagger/index.html时,我得到了一个工作的swagger页面,但当我试图使用我的任何控制器,甚至是默认的天气控制器时,它只是运行并停留在那里,说loading when。如果我在控制器中设置断点,它就会被击中。如果我打开浏览器调试工具,并停止它,它将显示“在调试器中暂停”,浏览器将闪烁,然后它将显示结果。

如果我转到https://localhost:44323/WeatherForecast,它会运行并给出正确的响应。

我在Visual studio中添加了该项目,选择blazor应用程序和=> Blazor WebAssembly应用程序作为新项目,并选择AspNetCore托管和渐进式Web应用程序。

我已通过nuget Swashbuckle.AspNetCore.SwaggerGen v5.5.0安装

Swashbuckle.AspNetCore.SwaggerUI v5.5.0

我的整个启动课是

代码语言:javascript
运行
复制
  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");
        });
    }
}
EN

回答 1

Stack Overflow用户

发布于 2021-09-18 15:19:23

我遇到了类似的问题,但是我能够通过在services.AddControllersWithViews();services.AddRazorPages();之后添加services.AddControllers();来解决这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62540840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档