---
title: "How to solve CORS error in .Net core API?"  
description: "How to solve CORS error in .Net core API?"  
author: "Steilla Mitchel"  
published: 2023-08-28  
updated: 2023-08-29  
canonical: https://www.mindstick.com/forum/159708/how-to-solve-cors-error-in-dot-net-core-api  
category: ".net core"  
tags: ["error", ".net core", ".net core api"]  
reading_time: 2 minutes  

---

# How to solve CORS error in .Net core API?

How to solve [CORS](https://www.mindstick.com/forum/159286/what-is-cors-and-why-might-you-need-to-handle-it-in-your-backend-code) [error](https://yourviews.mindstick.com/view/88527/fixing-quickbooks-error-4120-reinstalling-vs-repairing) in .Net [core API](https://www.mindstick.com/forum/160547/how-to-pass-multiple-parameters-in-url-dot-net-core-api)?

## Replies

### Reply by Aryan Kumar

CORS stands for Cross-Origin Resource Sharing. It is a security feature that prevents a web application from accessing resources from another domain. However, sometimes you may need to allow cross-origin requests, such as when you are using a JavaScript framework to build your application.

To solve CORS errors in .NET Core [API](https://www.mindstick.com/articles/12641/instagram-api-upgraded-to-facebook-graph), you need to enable CORS in your application. You can do this by following these steps:

1. Install the `Microsoft.AspNetCore.Cors` package.
2. Add the CORS service to the `ConfigureServices()` method in your Startup class.
3. Apply the CORS policy in the `Configure()` method in your Startup class.

The following code shows how to enable CORS in a .NET Core API:

C#

```plaintext
// Install the CORS package.
Install-Package Microsoft.AspNetCore.Cors -Version 3.1.0

// Add the CORS service to the ConfigureServices() method.
services.AddCors(options => {
  options.AddPolicy("AllowAll", builder => {
    builder.AllowAnyOrigin();
    builder.AllowAnyHeader();
    builder.AllowAnyMethod();
  });
});

// Apply the CORS policy in the Configure() method.
app.UseCors("AllowAll");
```

The `AllowAll` policy allows all origins, headers, and methods. You can customize the policy to allow specific origins, headers, and methods.

Once you have enabled CORS, you should no longer get CORS errors.

Here are some additional things to keep in mind about CORS in .NET Core API:

- The `AllowAll` policy is not recommended for production applications. It is better to specify the specific origins, headers, and methods that you want to allow.
- The CORS policy is applied to all requests by default. If you want to apply the policy to specific requests, you can use the `UseCors()` middleware in the `Configure` method.
- The CORS policy can be applied to individual controllers or actions. To do this, you can use the `[EnableCors]` attribute on the controller or action.


---

Original Source: https://www.mindstick.com/forum/159708/how-to-solve-cors-error-in-dot-net-core-api

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
