Rakshith Murukannappa
Rakshith Murukannappa

Reputation: 589

.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. even though I have enabled CORS in my ConfigureServices Method and used the policy in the Configure method in the startup

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?

Upvotes: 2

Views: 3627

Answers (2)

Theja Wijesiriwardane
Theja Wijesiriwardane

Reputation: 172

This might help,

Note: The URL must not contain a trailing slash (/). If the URL terminates with /, the comparison returns false and no header is returned.

Upvotes: 0

Rakshith Murukannappa
Rakshith Murukannappa

Reputation: 589

So, Basically as CRice and johnny commented, Internal server error are propagated as CORS Error.

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.

It can be understood better here

Upvotes: 1

Related Questions