我在使用outputcache和urlrewriting时遇到了问题。我们有一个应用程序可以将url (IE http://localhost/about/)重写为"~/page.aspx“。根据URL (/about/),我们确定要显示的内容。
现在我们尝试将outputcache添加到该页面:
< %@ 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使缓存失效?或者,我们是否应该为每个页面提供一个缓存键或其他东西,以便能够以编程方式使缓存无效?
提前感谢!
发布于 2011-03-31 18:01:42
您不需要使缓存无效。您可以告诉服务器不要对某些请求使用缓存。
要在页面加载中做到这一点,请添加以下内容
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);发布于 2014-12-22 05:02:08
我已经用下面的链接解决了问题。http://www.superstarcoders.com/blogs/posts/making-asp-net-output-cache-work-with-post-back.aspx
基本上是在我的用户控制上;
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);
}https://stackoverflow.com/questions/5496393
复制相似问题