Reputation: 218732
I have an ASP.NET website where i have implemented page level caching using the OutPutCache directive.This boosted the page performance.My pages has few parts(Some buttons,links and labels) which are specific to the logged in user.If user is not logged in,they will see different links.Now Since i implemented the page level caching,Even after the user logged in,It's showing the old page content(Links and buttons meant for the Non logged in User).
Caching is obviously good.But how to get rid of this problem ? Do i need to completely remove caching ?
Upvotes: 0
Views: 514
Reputation: 8926
You can use the VaryByParam directive:
VaryByParam: This attribute allows us to control how many cached versions of the page should be created based on name/value pairs sent through HTTP POST/GET. The default value is None. None implies that only one version of the page is added to the Cache, and all HTTP GET/POST parameters are simply ignored. The opposite of the None value is *. The asterisk implies that all name/value pairs passed in are to be used to create cached versions of the page. The granularity can be controlled, however, by naming parameters (multiple parameter names are separated using semi-colons).
Used like so in the page directive
<%@ OutputCache Duration="10800" VaryByParam="State;City" %>
Be careful what you use in the VaryByParam, as this can cause the number of copies of the page in memory to be up to the number of different values of your parameter that exist.
EDIT: as mentioned in comments, this won't work if you're using cookies for login, but some people do use cookie-less login, which puts the info in the GET/POST portion.
See here for more details
Upvotes: 2
Reputation: 1319
what you want is Partial Page caching: http://msdn.microsoft.com/en-us/library/ms227429.aspx and http://msdn.microsoft.com/en-us/library/h30h475z.aspx
Upvotes: 3
Reputation: 37547
I ran into the exact same issue and was able to resolve it using Response.WriteSubstitution
. Just create a static method that accepts HttpContext
as an argument, returns the login status as a string, and render the method using WriteSubstitution
:
Response.WriteSubstitution(new HttpResponseSubstitutionCallback(GetLoginStatus));
The rest of the page will cache as normal but the login status will be updated each time the page is loaded.
Upvotes: 2