Ask Your Question
0

How can I configure an ASP.NET Core Web API to allow any route, method, and verb?

asked 2022-03-26 11:00:00 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-06-15 19:00:00 +0000

ladyg gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-03-26 11:00:00 +0000

Seen: 8 times

Last updated: Jun 15 '21