发布请求首先失败,然后响应

时间:2020-01-15 14:29:56

标签: angular asp.net-core http-post signalr http-status-code-200

使用angular 8作为客户端,.NetCore 2.2 Web API,当我尝试进行发布请求时,它首先请求状态为204的选项,然后它请求发布请求,该请求被发送到服务器,我可以对其进行跟踪在控制器中,但是当控制器操作要返回数据时,它将失败。有趣的是,如果我再次毫不延迟地发送相同的帖子请求,它将成功!

In this image会立即调用与控制器相同的动作。

我必须补充一点,我在两次请求之间使用Signalr,并且我更改了一些cors政策,这与它有什么关系吗?:

namespace testAPI
{
    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.
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddSignalR();
            services.AddSingleton<IChatRoomService, ChatController>();
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);



            services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
            {
                //before signalr
                //builder.AllowAnyOrigin()
                //       .AllowAnyMethod()
                //       .AllowAnyHeader();

                //after signalr
                builder
                .AllowAnyHeader()
                .AllowAnyMethod()
                .SetIsOriginAllowed(_ => true)
                .AllowCredentials();
            }));

            services.Configure<BrotliCompressionProviderOptions>(options =>
            {
                options.Level = CompressionLevel.Optimal;
            });

            services.Configure<GzipCompressionProviderOptions>(options =>
            {
                options.Level = CompressionLevel.Optimal;
            });



            services.AddResponseCompression(options =>
            {

                options.Providers.Add<BrotliCompressionProvider>();
                options.Providers.Add<GzipCompressionProvider>();
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseResponseCompression();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {

                app.UseHsts();
            }



            app.UseHttpsRedirection();
            #region SignalR

            app.UseCors("MyPolicy");   //after signalr
            app.UseSignalR(routes =>
            {
                routes.MapHub<ChatHub>("/chat");
                routes.MapHub<ChatAdminHub>("/chat/admin");

            });

            #endregion
            app.UseMvc();





        }
    }
}

0 个答案:

没有答案