【问题标题】:Basing ASP.NET Outputcache on querystring parameter AND session基于查询字符串参数和会话的 ASP.NET 输出缓存
【发布时间】:2011-01-23 08:15:38
【问题描述】:

我想知道是否可以将输出缓存与查询字符串参数和会话参数一起使用。

我提供基于位置的内容,countryid 存储在 session 中,而 categoryid、pageindex 等其他参数存储在 querystring 中。

【问题讨论】:

    标签: c# asp.net caching


    【解决方案1】:

    使用 VaryByCustom 可以根据您想要的几乎任何内容来改变输出缓存,并提供一个返回缓存键字符串的特殊函数。对于您的情况,请尝试这样的指令:

    <%@ OutputCache Duration="30" VaryByParam="myParam" VaryByCustom="mySessionVar" %>
    

    然后在 Global.asax 中,为您的应用程序覆盖 GetVaryByCustomString 函数:

    public override string GetVaryByCustomString(HttpContext context, string arg)
    {
        if(arg == "mySessionVar" && Session["mySessionVar"] != null)
        {
            return Session["mySessionVar"].ToString();
        }
    
         return base.GetVaryByCustomString(context, arg);
    }
    

    【讨论】:

    • -1 此时您无法访问会话。 “会话状态在此上下文中不可用。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    相关资源
    最近更新 更多