Access to font at '....woff2' from origin 'https:...' has been blocked by CORS policy: No 'Access-Control-Allow-Origin'

Nguyen Ngoc Chuc 0 Reputation points
2023-01-15T09:38:25.0633333+00:00

I'm using Asp.net Core 6 with 2 projects:

  1. Static: https://localhost:6402/
  2. App: https://localhost:6403/

I'm getting this error, but I don't know how to fix it, I hope someone can help.

Access to font at 'https://localhost:6402/theme/01/plugins/global/fonts/@fortawesome/fa-solid-900.woff2' from origin 'https://localhost:6403' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

In the static project I added:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCors(options =>
{
    options.AddDefaultPolicy(
        policy =>
        {

            policy.AllowAnyOrigin()

            .AllowAnyHeader()

            .AllowAnyMethod();
        });
});
var app = builder.Build();
app.UseStaticFiles();
app.UseCors();
app.MapGet("/", () => "Running...!");
app.Run();

and i also tried with web.config

<?xml version="1.0" encoding="UTF-8"?>
<system.webServer>
    <httpProtocol>
        <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*" />
        </customHeaders>
    </httpProtocol>
</system.webServer>

But it still doesn't work, what should I do now?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,166 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Nguyen Ngoc Chuc 0 Reputation points
    2023-01-15T13:39:21.19+00:00

    I solved the problem, just move app.UseCors(); above app.UseStaticFiles();

    var app = builder.Build();
    
    app.UseCors(); 
    app.UseStaticFiles();
    app.MapGet("/", () => "Running...!"); 
    app.Run();
    
    
    0 comments No comments