简体   繁体   English

.NET Core - 即使在启用 CORS 后也出现 CORS 错误

[英].NET Core - CORS error even after enabling CORS

I am trying to call the WebAPI method to get some data, but it throws a Cross-Origin Read Blocking (CORB) blocked cross-origin response https://localhost:44332/api/Controller/Action with MIME type text/html.我正在尝试调用 WebAPI 方法来获取一些数据,但它抛出了一个Cross-Origin Read Blocking (CORB) blocked cross-origin response https://localhost:44332/api/Controller/Action with MIME type text/html. even though I have enabled CORS in my ConfigureServices Method and used the policy in the Configure method in the startup即使我在 ConfigureServices 方法中启用了 CORS 并在启动时使用了 Configure 方法中的策略

public void ConfigureServices(IServiceCollection services)
        {

            services.AddCors(options =>
            {
                options.AddPolicy("AllowAll",
                    builder =>
                    {
                        builder
                            .WithOrigins("http://localhost:4200/")
                            .AllowAnyMethod()
                            .AllowAnyHeader()
                            .AllowCredentials();
                    });
            });

            services.AddMvc();
        }

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            .....
            app.UseCors("AllowAll");
            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });


}

What could the reason be?原因可能是什么?

So, Basically as CRice and johnny commented, Internal server error are propagated as CORS Error.因此,基本上正如 CRice 和 johnny 评论的那样, Internal server error作为 CORS 错误传播。

Since it is good practice to handle all errors within the application, doing so will make sure 500 Internal Server Error will be intact even in the case of an exception and not be masked as CORS Error.由于处理应用程序中的所有错误是一种很好的做法,因此这样做将确保500 Internal Server Error即使在出现异常的情况下也将完好无损,并且不会被屏蔽为 CORS 错误。

It can be understood better here在这里可以更好地理解

This might help,这可能会有所帮助,

Note: The URL must not contain a trailing slash (/).注意:URL 不得包含尾部斜杠 (/)。 If the URL terminates with /, the comparison returns false and no header is returned.如果 URL 以 / 结尾,则比较返回 false 并且不返回标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM