Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To configure an ASP.NET Core Web API to allow any route, method, and verb, you can add the following code in the ConfigureServices method in the Startup.cs file:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddCors(options =>
    {
        options.AddPolicy("AllowAll",
            builder =>
            {
                builder
                    .AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader();
            });
    });
}

Then add the following code in the Configure method in the same file:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseCors("AllowAll");
    app.UseRouting();        
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}

This will allow any origin (i.e., allow requests from any domain), any method (i.e., allow GET, POST, PUT, DELETE, etc.), and any header (i.e., allow any HTTP headers) for any route in the Web API.