I have a simple ASP.NET Core 2.2 Web Api controller:
[ApiVersion("1.0")]
[Route("api/[controller]")]
[ApiController]
public class TestScenariosController : Controller
{
[HttpGet("v2")]
public ActionResult<List<TestScenarioItem>> GetAll()
{
var entities = _dbContext.TestScenarios.AsNoTracking().Select(e => new TestScenarioItem
{
Id = e.Id,
Name = e.Name,
Description = e.Description,
}).ToList();
return entities;
}
}
When I query this action from angular app using @angular/common/http
:
this.http.get<TestScenarioItem[]>(`${this.baseUrl}/api/TestScenarios/v2`);
In IE11, I only get the cached result.
How do I disable cache for all web api responses?
You can add ResponseCacheAttribute
to the controller, like this:
[ApiVersion("1.0")]
[Route("api/[controller]")]
[ApiController]
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
public class TestScenariosController : Controller
{
...
}
Alternatively, you can add ResponseCacheAttribute
as a global filter, like this:
services
.AddMvc(o =>
{
o.Filters.Add(new ResponseCacheAttribute { NoStore = true, Location = ResponseCacheLocation.None });
});
This disables all caching for MVC requests and can be overridden per controller/action by applying ResponseCacheAttribute
again to the desired controller/action.
See ResponseCache attribute in the docs for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With