Like the title says, why does a permanent 301 redirect displays a cached version of the page while a temporary 302 doesn't ?
How can I force the permanent 301 redirect to always display without a cache ?
In my file Program.cs, I intercept the current request url path and do logic on it
app.Use(async (context, next) =>
{
string strUrlPath = context.Request.Path.ToString().ToLowerInvariant();
switch (strUrlPath)
{
case string _ when strUrlPath.StartsWith("/deprecated_page"):
{
// Do some logic on number of times it was visited, insert into Database
// Do some logging on who attempted to view this page, ...etc.
Log.LogContext().Warning(...);
// redirect to 404 page
context.Response.Redirect("/404", permanent: true);
return;
}
default:
await next();
break;
}
});
When the boolean flag permanent: true is passed as an argument, the page will first do logging but subsequent request will be cached and none of the logic nor the logging will be executed.
If however the flag is set to permanent: false and the redirect is temporary, all logic and logging is done on each request.
Why is this a behaviour and how can I force to disable cache server side on a permanent 301 redirect ?
Because that's what "permanent" and "temporary" mean.
The purpose of a permanent redirect is to tell the client to always redirect to the target, so it never needs to check again.
If you want clients to always make the first request, then you need to give them a temporary redirect, so they have to check again every time.