I know I can setup OutputCacheProfiles at web.config file.
I like to know how to apply different cache profile to different user role on page (controller) level?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can decorate a controller with the OutputCache attribute which allows arguments to be passed as parameters. For example;
There is no reason why you couldn’t extend the attribute to take a further argument “RoleName” and perform a “Roles.IsUserInRole(RoleName)” and load different settings based upon each role.
EDIT
After comments from the author, I have reviewed my solution.
Firstly, you can define you cache profiles within the Web.config;
I have extended the OutputCacheAttribute to account for authorisation of a user, and if the user authenticates, it loads that CacheProfile;
Here is the Index.cshtml file for completeness;
Note: You will have to make sure to define a cacheprofile for each of your roles, aswell as a default for when no role is found.
EDIT
The author wished to know how to set the cache profile within the controller, I have posted a viable solution, but I don’t like it because of the use of HttpContext.Items – so if anyone can suggest alternatives?
Firstly, you must change the OnActionExecuting to OnActionExecuted;
The following line allows you to set the profile within the controller;
Let me know if I can be of further assistance,
Matt