c# - Aspire.StackExchange.Redis.OutputCaching cors error for cache data - Stack Overflow

admin2025-04-26  3

The Redis output cache in Aspire does not support policies. When I retrieve a cached response in my SPA application, I encounter a CORS error:

Request Details:

URL: url Method: GET Status Code: 200 OK Referrer Policy: strict-origin-when-cross-origin

Access to XMLHttpRequest at 'url' from origin 'url' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I have already enabled the CORS policy in my application and don’t face any issues with other controller actions. Here's the relevant configuration:

const string policy = "CorsPolicy";
builder.Services.AddCors(options =>
{
    options.AddPolicy(policy, builder =>
    {
        builder.AllowAnyHeader();
        builder.AllowAnyMethod();
        builder.AllowAnyOrigin();
    });
});

var app = builder.Build();

app.UseOutputCache();

app.MapDefaultEndpoints();

app.UseMiddleware<BehaviorMiddleware>();

app.UseHttpsRedirection();
app.UseCors(policy);
app.UseAuthorization();

app.MapControllers();

app.Run();
[HttpGet("")]
[OutputCache(Duration = 120)]
public async Task<IActionResult> GetVendors([FromQuery] GetUserCurrentGridModel query, CancellationToken cancellationToken = default)
{
    return Ok();
}

The Redis output cache in Aspire does not support policies. When I retrieve a cached response in my SPA application, I encounter a CORS error:

Request Details:

URL: url Method: GET Status Code: 200 OK Referrer Policy: strict-origin-when-cross-origin

Access to XMLHttpRequest at 'url' from origin 'url' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I have already enabled the CORS policy in my application and don’t face any issues with other controller actions. Here's the relevant configuration:

const string policy = "CorsPolicy";
builder.Services.AddCors(options =>
{
    options.AddPolicy(policy, builder =>
    {
        builder.AllowAnyHeader();
        builder.AllowAnyMethod();
        builder.AllowAnyOrigin();
    });
});

var app = builder.Build();

app.UseOutputCache();

app.MapDefaultEndpoints();

app.UseMiddleware<BehaviorMiddleware>();

app.UseHttpsRedirection();
app.UseCors(policy);
app.UseAuthorization();

app.MapControllers();

app.Run();
[HttpGet("")]
[OutputCache(Duration = 120)]
public async Task<IActionResult> GetVendors([FromQuery] GetUserCurrentGridModel query, CancellationToken cancellationToken = default)
{
    return Ok();
}
Share Improve this question edited Mar 3 at 9:45 Lex Li 63.5k11 gold badges124 silver badges161 bronze badges asked Jan 14 at 16:43 mohammadmahdi Talachimohammadmahdi Talachi 6512 gold badges8 silver badges21 bronze badges 3
  • 1 I suggest you could make sure app.UseCors(policy); is called before app.UseOutputCache() and try again. – Brando Zhang Commented Jan 15 at 5:57
  • @BrandoZhang it works thanks – mohammadmahdi Talachi Commented Jan 15 at 21:24
  • I will write an answer as the reply, please mark it, so that it will help other folks who face the same error could find the solution more easily . – Brando Zhang Commented Jan 16 at 1:32
Add a comment  | 

1 Answer 1

Reset to default 0

According to your codes, this is related with how your CORS middleware executed, since the CORS middleware is executed after the cache middleware, which caused the issue.

You should make sure app.UseCors(policy); is called before app.UseOutputCache(), and then it will work well.

// Configure the HTTP request pipeline.

// Ensure CORS middleware is called first
app.UseCors(policy); 

// Call the output cache middleware after CORS
app.UseOutputCache();    
转载请注明原文地址:http://anycun.com/QandA/1745623750a91033.html