cicatrix
cicatrix

Reputation: 163

How to prevent a browser for caching a link?

I have a user private area and a 'Log off' link:

<a href="~/User/Logoff">Log off</a>

The problem is that the browser caches this link and does not make a request to the server each time users click on it.

I searched and found a solution that is supposed to turn off caching for the entire page:

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"/>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Expires" content="0"/>

But I do not want to turn off caching completely, I only want to make sure that when a user clicks 'Log off' the request would always be made.

Is it possible to tell the browser that this particular link is not cacheable?

Upvotes: 1

Views: 1872

Answers (1)

Taylor Wood
Taylor Wood

Reputation: 16194

Generally, state-changing HTTP operations shouldn't use the GET verb (like a link). You should consider using a <form> that POSTs to an endpoint that logs the user out.

<form method="POST" action="~/User/Logoff">
  <button type="submit">Log off</button>
</form>

This will never be cached.

If you must use the GET verb, and you're using WebAPI, I believe there are attributes you can decorate your route method with that will disable caching.

Upvotes: 2

Related Questions