Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6841009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:54:50+00:00 2026-05-26T23:54:50+00:00

I know I can setup OutputCacheProfiles at web.config file. I like to know how

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:54:50+00:00Added an answer on May 26, 2026 at 11:54 pm

    You can decorate a controller with the OutputCache attribute which allows arguments to be passed as parameters. For example;

    [OutputCache(Duration = 3600, VaryByParam = "None")]
    

    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;

    <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <clear />
    
          <add name="Default" duration="60" />
          <add name="Admin" duration="10" />
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>
    

    I have extended the OutputCacheAttribute to account for authorisation of a user, and if the user authenticates, it loads that CacheProfile;

    public class AuthorisedOutputCache : OutputCacheAttribute
    {
      public string RoleName { get; set; }
    
      public override void OnActionExecuting(ActionExecutingContext filterContext)
      {
        // Default Profile.
        CacheProfile = "Default";
    
        if (HttpContext.Current.Request.IsAuthenticated)
        {
          if (Roles.IsUserInRole(RoleName))
          {
            CacheProfile = RoleName;
          }
        }
    
        base.OnActionExecuting(filterContext);
      }
    }
    

    Here is the Index.cshtml file for completeness;

    @model DateTime
    
    @{
      ViewBag.Title = "Index";
    }
    
    <h2>Index</h2>
    
    <p>
      The time is @Model.TimeOfDay.ToString()
    </p>
    

    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;

    public class AuthorisedOutputCache : OutputCacheAttribute
    {
      public string RoleName { get; set; }
    
      public override void OnActionExecuted(ActionExecutedContext filterContext)
      {
        // Do you wish to force the profile?
        if (HttpContext.Current.Items["Cache.Force"] != null)
        {
          // Force the profile and remove the flag.
          CacheProfile = HttpContext.Current.Items["Cache.Force"].ToString();
          HttpContext.Current.Items.Remove("Cache.Force");
        }
        else
        {
          // If the profile has not been set - use the role based authorisation - 
          // otherwise, carry on as normal.
          if (string.IsNullOrEmpty(CacheProfile))
          {
            CacheProfile = "Default";
    
            if (HttpContext.Current.Request.IsAuthenticated)
            {
              if (Roles.IsUserInRole(RoleName))
              {
                CacheProfile = "Admin";
              }
            }
          }
        }
    
        base.OnActionExecuted(filterContext);
      }
    } 
    

    The following line allows you to set the profile within the controller;

    public ActionResult Index()
    {
      // Forces the cache profile to one of the name of "Mandatory".
      HttpContext.Items["Cache.Force"] = "Mandatory";
    
      return View(IndexViewName, DateTime.Now);
    }
    

    Let me know if I can be of further assistance,

    Matt

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can create setup file in InstallShield 2009 I don't know how to add
I know Installers can setup File Associations for your Application during the installation process,
As you probably know, you can setup date format in the Settings of an
how can I setup my app to accept documents via iTunes, like the Air
I know how to setup a route to a url like http://siteurl.com/mycustomaccount/dashboard , but
I set up 404 handler page in web.config, but it works ONLY when extension
Does anyone know where in CKEditor I can setup, so all links added will
I know you can use Reporting and Logging wizard to setup a report e-mail
I don't know in which way we can setup a plane surface that is
I just want to know how can a hacker setup a reverse proxy in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.