Search code examples
c#asp.netasp.net-coredotnet-aspire

C# Aspire configure CORS


I'm relatively new to setting up CORS in ASP.NET Core, and I'm running into some issues. I need to configure CORS for the Aspire orchestrator, but I'm not entirely sure how to do it correctly. I would really appreciate any advice or examples that could help me set it up properly for my project. Thanks in advance!

I tried adding a basic CORS configuration in the Startup.cs file of my ASP.NET Core project, allowing all origins, methods, and headers. I realize that this might not be the most appropriate solution, and I need to find a more suitable way to configure CORS for this scenario.

I need to clarify that this is specifically for projects launched by Aspire.


Solution

  • var webClient = builder
        .AddNpmApp("client", "../../path", "dev")
        .WithEnvironment("BROWSER", "none")
        .WithHttpEndpoint(env: "VITE_PORT")
        .WithExternalHttpEndpoints();
    
    // #2
    var api= builder.AddProject<Projects.Api_Web>("api")
        .WithReference(db)
        .WithReference(webClient)
        .WithExternalHttpEndpoints();
    
    // #3 
    var endpoint = api.GetEndpoint("http");
    webClient.WithEnvironment("VITE_API", endpoint);
    

    In this sequence, everything works as I want it to.