Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can decorate a controller with the OutputCache attribute which allows arguments to be passed as parameters. For example;</p> <pre><code>[OutputCache(Duration = 3600, VaryByParam = "None")] </code></pre> <p>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.</p> <p>EDIT</p> <p>After comments from the author, I have reviewed my solution.</p> <p>Firstly, you can define you cache profiles within the Web.config;</p> <pre><code>&lt;caching&gt; &lt;outputCacheSettings&gt; &lt;outputCacheProfiles&gt; &lt;clear /&gt; &lt;add name="Default" duration="60" /&gt; &lt;add name="Admin" duration="10" /&gt; &lt;/outputCacheProfiles&gt; &lt;/outputCacheSettings&gt; &lt;/caching&gt; </code></pre> <p>I have extended the OutputCacheAttribute to account for authorisation of a user, and if the user authenticates, it loads that CacheProfile;</p> <pre><code>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); } } </code></pre> <p>Here is the Index.cshtml file for completeness;</p> <pre><code>@model DateTime @{ ViewBag.Title = "Index"; } &lt;h2&gt;Index&lt;/h2&gt; &lt;p&gt; The time is @Model.TimeOfDay.ToString() &lt;/p&gt; </code></pre> <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.</p> <p>EDIT</p> <p>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?</p> <p>Firstly, you must change the OnActionExecuting to OnActionExecuted;</p> <pre><code>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); } } </code></pre> <p>The following line allows you to set the profile within the controller;</p> <pre><code>public ActionResult Index() { // Forces the cache profile to one of the name of "Mandatory". HttpContext.Items["Cache.Force"] = "Mandatory"; return View(IndexViewName, DateTime.Now); } </code></pre> <p>Let me know if I can be of further assistance,</p> <p>Matt </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload