2013-04-01 26 views
1

我有一个名称为uc_Menu.ascx的用户控件。 menues动态加载Role基地。所以,我将它包含在一个用户控件中并像这样缓存它;
注意:为了测试目的,我只提供了60秒的缓存;在Asp.Net中清除用户控件的输出缓存

<%@ OutputCache Duration="60" VaryByParam="none" %> 
<td id="Menu"> 
    <div id="firstpane" runat="server" class="menu_list"> 
     <p class="menu_head"> 
      <a href="/forms/Dashboard.aspx"> 
       <span style="background: url('/App_Themes/Default/Images/icons/ic_grid.png') no-repeat"></span> 
       DashBoard 
      </a> 
     </p> 
    </div> 
</td> 

背后的代码看起来像这样;

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
    { 
     BulitMenus(); 
     Response.Write("<h1>" + DateTime.Now.ToString() + "</h1>"); 
    } 
} 

我的母版页,我已经参考了上述用户控件;现在

<%@ Register Src="~/UserControls/uc_Menu.ascx" TagPrefix="Ken" TagName="Menu" %> 
<Ken:Menu runat="server" id="Menus" /> 

,当我测试了这一点,它工作正常(当加载页面时,第一次,缓存工作)
不过,我想清楚了,当上LogOff用户点击按钮(上高速缓存一个母版页),因为菜单是基于角色的,管理员可能会给用户更多的权限。在这种情况下,有必要重新编译菜单。 那么,我如何清除用户控件中的缓存;
注意:我也试过这个,但它没有工作; http://aspalliance.com/668_Remove_ASPNET_Page_Output_Cache_Entries

protected void Lbtn_LogOff_Click(object sender, EventArgs e) 
{ 

    HttpResponse.RemoveOutputCacheItem("/UserControls/uc_Menu.ascx"); 
    // this code is copied from a url above which i have included i my question 
    HttpContext.Current.Cache.Insert("Pages", DateTime.Now, null, 
     System.DateTime.MaxValue, System.TimeSpan.Zero, 
     System.Web.Caching.CacheItemPriority.NotRemovable, null); 

    FormsAuthentication.SignOut(); 
    Response.Redirect("~/authorization/Login.aspx"); 
} 
+0

将输出缓存用于基于角色的控件并不是个好主意,尤其是在VaryByParam =“none”的情况下。在你的情况下,一旦菜单缓存为管理员,它将以相同的方式显示给用户。例如http://stackoverflow.com/questions/290098/asp-net-mvc-caching-vary-by-authentication或http://stackoverflow.com/questions/2109928/how-to-turn-output-caching-关闭认证用户在asp-net-mvc等 – Lanorkin

回答