简体   繁体   English

为什么一个URL的页面使用另一个URL的缓存页面?

[英]Why is the page at one URL using the cached page from another URL?

Background 背景

I'm pretty new to ASP.NET and very new to the concept of caching. 我对ASP.NET还是很陌生,对缓存的概念也很陌生。 I'm hoping that for someone who knows that they're doing, the problem here will be obvious. 我希望对于知道自己正在做的人来说,这里的问题将很明显。

Using MCMS 2002, I built a template.aspx page that receives a "person_id" query string and builds pages accordingly. 使用MCMS 2002,我构建了一个template.aspx页面,该页面接收“ person_id”查询字符串并相应地构建页面。 Several different departments at the school where I work are using this template to display faculty biographies. 我工作所在学校的几个不同部门正在使用此模板来显示教师简历。 This much is all working correctly. 这一切都正常工作。

The Problem 问题

The problems start happening when I try caching the page. 当我尝试缓存页面时,问题开始发生。 When a person visits person_id=16175 on one department's page, then visits the same numeric bio on a different department page, it loads the cached page instead of rebuilding it. 当某人在一个部门的页面上访问person_id = 16175,然后在另一个部门的页面上访问相同的数字简历时,它将加载缓存的页面,而不是对其进行重建。 The problem, then, is that it has all of the wrong department branding. 因此,问题在于它的所有部门品牌错误。 For example: 例如:

http://health.usf.edu/medicine/obgyn/facstaf/profiles.htm?person_id=16175 http://health.usf.edu/medicine/surgery/surgery_bios.html?person_id=16175 http://health.usf.edu/medicine/obgyn/facstaf/profiles.htm?person_id=16175 http://health.usf.edu/medicine/surgery/surgery_bios.html?person_id=16175

It's the same person_id, but the URLs are obviously different. 这是相同的person_id,但网址显然不同。 It would be great if the template would recognize the different URLs and ignore the cache. 如果模板可以识别不同的URL并忽略缓存,那就太好了。 I'm assuming that the problem lies in the fact that both pages are being built using the same aspx page on the backend. 我假设问题出在两个页面都是使用后端的相同aspx页面构建的。 Here's the OutputCache bit from the aspx page: 这是aspx页面上的OutputCache位:

<%@ OutputCache Duration="86400" Location="Server" VaryByParam="person_id; section" VaryByCustom="CMSPosting" VaryByHeader="Referer" %>

I assume I'm doing something wrong here. 我想我在这里做错了。 Hopefully it will be obvious to someone who knows that they're doing. 希望对知道自己正在做的人很明显。 If you need more info, don't hesitate to ask. 如果您需要更多信息,请随时询问。 Thanks! 谢谢!

Your are most likely correct in your assumption about it being because they use the same ASPX file, I have experienced the same firsthand. 您的假设很可能是正确的,因为它们使用相同的ASPX文件,而我也曾经历过相同的第一手资料。 To get around this issue try the following. 要解决此问题,请尝试以下操作。

Try this for your output cache directive 为您的输出缓存指令尝试此操作

<%@ OutputCache Location="Server" VaryByCustom="url" Duration="60" VaryByParam="*" %>

and then add this in your Global.asax file. 然后将其添加到您的Global.asax文件中。

public override string GetVaryByCustomString(HttpContext context,string arg)
{
    if (arg == "url")
    {
         return context.Request.RawUrl;
    }
    return context.Request.RawUrl;//you can vary by other stuff here
}

A version of the page will be cached for every unique value returned by the method for a given argument. 对于给定参数,该方法返回的每个唯一值都将缓存该页面的版本。 So by returning the URL you ensure the cache will not be hit for different URLs 因此,通过返回URL,您可以确保不会为其他URL命中缓存

A key thing to note is that if you do not have a single physical file resolving to multiple URLs then VaryByParam="*" would be enough to get your desired behaviour. 需要注意的关键是,如果您没有一个解析为多个URL的物理文件,那么VaryByParam="*"就足以实现所需的行为。 It's because you have this URL routing that you require VaryByCustom="url" 这是因为您具有此URL路由,因此需要VaryByCustom="url"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM