---
title: "How to enable cors in ASP.NET Core 6.0 Web API project?"  
description: "How to enable cors in ASP.NET Core 6.0 Web API project?"  
author: "Utpal Vishwas"  
published: 2023-11-07  
updated: 2023-11-08  
canonical: https://www.mindstick.com/forum/160450/how-to-enable-cors-in-asp-dot-net-core-6-0-web-api-project  
category: ".net core"  
tags: ["asp.net core", ".net core", ".net core 6"]  
reading_time: 2 minutes  

---

# How to enable cors in ASP.NET Core 6.0 Web API project?

How to enable [cors](https://www.mindstick.com/forum/159286/what-is-cors-and-why-might-you-need-to-handle-it-in-your-backend-code) in [ASP.NET Core](https://www.mindstick.com/articles/12946/get-started-with-asp-dot-net-core-mvc-and-visual-studio) 6.0 [Web API](https://www.mindstick.com/articles/324352/how-to-create-web-api-in-dot-net-core-3-1-mvc) [project](https://www.mindstick.com/articles/105927/how-to-excel-at-managing-multiple-projects)?

## Replies

### Reply by Aryan Kumar

Enabling Cross-Origin Resource Sharing (CORS) in an [ASP.NET](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) Core 6.0 [Web](https://www.mindstick.com/articles/12783/the-ultimate-bunch-of-free-web-design-resources) [API](https://www.mindstick.com/articles/12641/instagram-api-upgraded-to-facebook-graph) project allows your API to respond to requests from different domains or origins. To enable CORS, you can follow these steps:

**Install the Microsoft.AspNetCore.Cors Package (if not already installed)**:

Make sure you have the **Microsoft.AspNetCore.Cors** package added to your project. If it's not already included, you can add it using the following command:

```plaintext
dotnet add package Microsoft.AspNetCore.Cors
```

**Configure CORS in the Startup.cs File**:

In your **Startup.cs** file, you need to configure CORS services in the **ConfigureServices** method and set up CORS policies in the **Configure** method.

csharpCopy code

```plaintext
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors(options =>
        {
            options.AddPolicy("AllowSpecificOrigin", builder =>
            {
                builder
                    .WithOrigins("http://example.com", "https://example.com")
                    .AllowAnyHeader()
                    .AllowAnyMethod();
            });
        });

        // Other services configuration
    }

    public void Configure(IApplicationBuilder app)
    {
        // ...

        app.UseCors("AllowSpecificOrigin"); // Apply the CORS policy

        // ...

        app.UseRouting();

        // ...

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}
```

In the code above:

- We've added CORS services by calling **services.AddCors** in the **ConfigureServices** method.
- We've configured a CORS policy named "AllowSpecificOrigin" that allows requests from "http://example.com" and "https://example.com." You can customize the allowed origins, headers, and methods according to your requirements.

**Apply the CORS Policy**:

To apply the CORS policy to your controllers or specific actions, you can use the **[EnableCors]** attribute:

```plaintext
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Cors;

[ApiController]
[Route("api/[controller]")]
public class MyController : ControllerBase
{
    [HttpGet]
    [EnableCors("AllowSpecificOrigin")] // Apply the CORS policy
    public IActionResult Get()
    {
        // Your action logic here
    }
}
```

In this example, the "AllowSpecificOrigin" CORS policy is applied to the **Get** action of the **MyController**.

With these configurations, your ASP.NET Core 6.0 Web API project is now set up to allow cross-origin requests from the specified domains. Be sure to customize the CORS policy to match your specific needs, such as allowing multiple origins, headers, or methods.


---

Original Source: https://www.mindstick.com/forum/160450/how-to-enable-cors-in-asp-dot-net-core-6-0-web-api-project

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
