I Have dotnet core 5
application and Angular app
that in local host
All Http methods
working well but on the web host HttpGet, HttpPost Methods
working correctly and HttpPut, DELETE
thrown Exception:
Access to XMLHttpRequest at '---' from origin '---' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
that config Cors
like bellow:
public static IServiceCollection AddAdminApiCors(this IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder =>
{
builder.AllowAnyOrigin();
builder.AllowAnyHeader();
builder.AllowAnyMethod();
});
});
return services;
}
in Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddAdminApiCors();
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseCors("CorsPolicy");
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
[Authorize]
[EnableCors("CorsPolicy")]
public class CompanyController : ControllerBase
{
[HttpGet]
public async Task<ActionResult<IEnumerable<CompanyViewModel>>> Get([FromQuery] CompanyQuery companyQuery)
{
return Ok(await _mediator.Send(companyQuery));
}
[HttpPost]
public async Task<ActionResult<int>> Create([FromBody] CreateCompanyCommand command)
{
var productId = await _mediator.Send(command);
return Ok(productId);
}
[HttpPut]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesDefaultResponseType]
public async Task<IActionResult> Update([FromBody] UpdateCompanyCommand command)
{
await _mediator.Send(command);
return NoContent();
}
[HttpDelete("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesDefaultResponseType]
public async Task<IActionResult> Delete(int id)
{
await _mediator.Send(new DeleteCompanyCommand { Id = id });
return NoContent();
}
}
in many posts told change web.config
file and remove WebDAV IIS Module
that i follow them but my problam not solved
cors put error