我做的一个系统中,有一个特殊的需要。站点绑定多个域名。
同一个页面(如 Default.aspx),不同的域名,页面内容是变化的。比如:Default.aspx.cs 中:string Url = 获取访问的域名;if (Url == "www.a.com")
{
装载 a.com 的页面内容;
}
else if (Url == "www.b.com")
{
装载 b.com 的页面内容;
}
......
但是页面缓存就出现问题了:它不区分域名缓存Response.Cache.SetExpires(DateTime.Now.AddSeconds(SitePageCacheSeconds));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.VaryByParams["*"] = true;
Response.Cache.SetValidUntilExpires(true);当我用 a.com 访问后,由于缓存在服务器上,其他用户用 b.com 访问,也可能是 a.com 的内容。---------------------问:怎么区分域名呢?
需要用 Response.Cache.SetVaryByCustom("...") 吗?
同一个页面(如 Default.aspx),不同的域名,页面内容是变化的。比如:Default.aspx.cs 中:string Url = 获取访问的域名;if (Url == "www.a.com")
{
装载 a.com 的页面内容;
}
else if (Url == "www.b.com")
{
装载 b.com 的页面内容;
}
......
但是页面缓存就出现问题了:它不区分域名缓存Response.Cache.SetExpires(DateTime.Now.AddSeconds(SitePageCacheSeconds));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.VaryByParams["*"] = true;
Response.Cache.SetValidUntilExpires(true);当我用 a.com 访问后,由于缓存在服务器上,其他用户用 b.com 访问,也可能是 a.com 的内容。---------------------问:怎么区分域名呢?
需要用 Response.Cache.SetVaryByCustom("...") 吗?
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.VaryByParams["*"] = true;
Response.Cache.SetValidUntilExpires(true);
Response.Cache.SetVaryByCustom("SiteUrl");
Global.asax.cs 中: public override string GetVaryByCustomString(HttpContext context, string arg)
{
if (arg == "SiteUrl")
{
return 获取站点的来访域名;
}
else
{
return "";
}
}
这样可以吗
{
//在应用程序启动时运行的代码 }
void Application_End(object sender, EventArgs e)
{
//在应用程序关闭时运行的代码 }
void Application_Error(object sender, EventArgs e)
{
//在出现未处理的错误时运行的代码 } void Session_Start(object sender, EventArgs e)
{
//在新会话启动时运行的代码 } void Session_End(object sender, EventArgs e)
{
//在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式
//设置为 StateServer 或 SQLServer,则不会引发该事件。 } public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (custom == "ddddd")
return context.Request.Url.Host;
return base.GetVaryByCustomString(context, custom);
}
</script>
<%@ OutputCache Duration="60000" VaryByParam="none" VaryByCustom="SiteUrl" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> protected void Page_Load(object sender, EventArgs e)
{
this.Label1.Text = DateTime.Now.ToLongTimeString();
}
</script><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" EnableViewState="False" Text="Label"></asp:Label>
</form>
</body>
</html>
在Global.asax中:
public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (custom == "SiteUrl")
return context.Request.Url.Host;
return base.GetVaryByCustomString(context, custom);
}