I was having a pretty heavy problem, I installed e WebAPI
on a corporate server IIS
of my company, and specifically, when I was trying to use e HTTPMethod
DELETE
in an application that consumes e API
, I was getting the following error:
Access to XMLHttpRequest at 'http://UrlDelApi' from origin 'http://UrlDelSitioWeb' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
At first I thought it might be a bug CORS
in my app, but I already had it set up. The configuration is as follows:
ConfigureServices
public readonly string _MyCors="CorsPolicy";
public void ConfigureServices(IServiceCollection services)
{
//...mucho código antes de esto
//agregamos el CORS para las peticiones HTTP desde el cliente.
services.AddCors(options =>
{
options.AddPolicy(name: _MyCors, builder =>
{
//recordatorio: permitir cualquier header, origen y método no es buena práctica
builder.AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin();
});
});
}
configure
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
/*
* Permitiremos a CORS que:
* Permita recibir peticiones de cualquier origen
* Permitir recibir peticiones para cualquier método
* Recibir cualquier encabezado en la petición
*/
app.UseCors(_MyCors);
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
What is missing?
Once verifying that the error did not come from the configuration of the
CORS
, since it specifically says that I allow any origin, method andheader
, I continued to investigate. I came to this post where I saw that there were already similar problems with this. They attribute the problem to a feature of the so-IIS
calledWebDav
. In principle, the accepted answer says that you should disable that feature of windows, which for me, is not possible. Browsing further into that question, I found this answer :With that, my app started working properly.
I decided to share this because it took me more than 4 hours to find a solution, so I hope that someone in the future will find it useful. Cheers! :D