首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >URL重写+ outputcache

URL重写+ outputcache
EN

Stack Overflow用户
提问于 2011-03-31 15:13:34
回答 2查看 1.7K关注 0票数 3

我在使用outputcache和urlrewriting时遇到了问题。我们有一个应用程序可以将url (IE http://localhost/about/)重写为"~/page.aspx“。根据URL (/about/),我们确定要显示的内容。

现在我们尝试将outputcache添加到该页面:

代码语言:javascript
复制
< %@ outputcache duration="600" location="Server" varybyparam="Custom" varybycustom="RawURL" %>

在Global.asax中,我们覆盖GetVaryByCustomString,如下所示:

代码语言:javascript
复制
public override string GetVaryByCustomString(HttpContext context, string custom)
{
    if (custom == "RawUrl")
    {
        return context.Request.RawUrl;
    }
    else
    {
        return string.Empty;
    }
}

然而,当我们发布页面时,我想让缓存失效,这样编辑器就可以直接看到更改。但无论我怎么尝试,似乎都不能使缓存无效。如果我想使"/about/“无效,我想这样做:

代码语言:javascript
复制
HttpResponse.RemoveOutputCacheItem("/about/");

不幸的是,这不起作用。唯一有效的方法是:

代码语言:javascript
复制
HttpResponse.RemoveOutputCacheItem("/page.aspx");

这将清除所有页面的缓存,而不仅仅是"/about/“。

有没有办法根据url使缓存失效?或者,我们是否应该为每个页面提供一个缓存键或其他东西,以便能够以编程方式使缓存无效?

提前感谢!

EN

回答 2

Stack Overflow用户

发布于 2011-03-31 18:01:42

您不需要使缓存无效。您可以告诉服务器不要对某些请求使用缓存。

要在页面加载中做到这一点,请添加以下内容

代码语言:javascript
复制
Response.Cache.AddValidationCallback((ValidateCache), Session);

然后添加此方法来告诉应用程序是否将输出缓存中的内容用于当前请求

代码语言:javascript
复制
    public static void ValidateCache(HttpContext context, Object data, ref HttpValidationStatus status)
    {

        bool isEditor = /*assignment here*/;
        if (!isEditor)
        {
            status = HttpValidationStatus.Valid;
        }
        else
        {
            status = HttpValidationStatus.IgnoreThisRequest;
        }
    }

如果您不希望缓存某个请求以供其他用户查看,请使用以下方法

代码语言:javascript
复制
 Response.Cache.SetCacheability(HttpCacheability.NoCache);
票数 0
EN

Stack Overflow用户

发布于 2014-12-22 05:02:08

我已经用下面的链接解决了问题。http://www.superstarcoders.com/blogs/posts/making-asp-net-output-cache-work-with-post-back.aspx

基本上是在我的用户控制上;

代码语言:javascript
复制
protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        if (Request.QueryString["url"] == null)
            return;
        string url = "www." + Request.QueryString["url"].ToString().Replace("http://", "").Replace("https://", "").Replace("www.", "").ToLower();


        Response.AddCacheItemDependency(url);
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5496393

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档