c# - URL重写+输出缓存

标签 c# .net asp.net url-rewriting outputcache

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

现在我们正尝试将输出缓存添加到该页面:

< %@ outputcache duration="600" location="Server" varybyparam="Custom" varybycustom="RawURL" %>

在 Global.asax 中,我们重写了 GetVaryByCustomString,如下所示:

public override string GetVaryByCustomString(HttpContext context, string custom)
{
    if (custom == "RawUrl")
    {
        return context.Request.RawUrl;
    }
    else
    {
        return string.Empty;
    }
}

但是,当我们发布页面时,我想使缓存失效,以便编辑直接看到更改。但无论我尝试什么,我似乎都无法使缓存无效。如果我想让“/about/”无效,我想这样做:

HttpResponse.RemoveOutputCacheItem("/about/");

不幸的是,这不起作用。唯一似乎有效的是:

HttpResponse.RemoveOutputCacheItem("/page.aspx");

这会清除我所有页面的缓存,而不仅仅是“/about/”。

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

提前致谢!

最佳答案

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

要在您的页面加载中执行此操作,请添加此

Response.Cache.AddValidationCallback((ValidateCache), Session);

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

    public static void ValidateCache(HttpContext context, Object data, ref HttpValidationStatus status)
    {

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

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

 Response.Cache.SetCacheability(HttpCacheability.NoCache);

关于c# - URL重写+输出缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5496393/

相关文章:

asp.net - ELMAH过滤不起作用

c# - Vb.net 中的字符串值 Try Catch

c# - 是什么 | (管道)在 C# 中是什么意思?

c# - 将文件拖放到 wpf/C# 应用程序时,如何维护 Windows 资源管理器中的文件顺序?

c# - 管理员和用户检查不起作用

asp.net - LINQ 查询中的 Case 语句 - 匿名类型到字符串?

c# - ASP.NET MVC4网页异步FTP上传

c# - OpenIdConnectAuthenticationHandler : message. 状态为 null 或为空

c# - 根据 Icollection 中的值对 Ienumerable 进行排序的正确 LINQ 语法

c# - ASP.NET Core Web API 跳过身份验证