我的数据库中有一个到期日期,我想在达到该页面的到期日期时重定向一个网页。
我该怎么做?
谢谢
您可以使用页面缓存来做到这一点。我显然不熟悉您如何存储过期日期,但我假设您有 [exp_date: url]。
所以:
protected void Application_Start(object sender, EventArgs e)
{
Dictionary<Datetime, string> pages = Read_from_database();
Context.Cache.Insert("ExpireCache", pages, new CacheDependency(m_strPath),
System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration,
CacheItemPriority.Default);
}
而在
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Request.Url.AbsolutePath == "page_expired.aspx")
{
return;
}
var cache = HttpContext.Current.Cache["ExpireCache"];
if (cache.ContainsKey(HttpContext.Current.Request.RawUrl) &&
cache[HttpContext.Current.Request.Url.AbsolutePath] < DateTime.Now)
{
HttpContext.Response.Current.Redirect("page_expired.aspx");
}
}
您还可以将 SqlDbDependency 添加到缓存中,以便在您修改数据库中的过期日期时更新它...
你可以Trigger
在你的数据库上放一个。在特定时间或动作后触发,然后测试日期以确保它没有过期。
如果它是这样一个简单的代码块就可以完成这项工作。
if (HttpContext.Current.Request.Url.ToString().ToLower().Contains(
"http://mySite.com"))
{
HttpContext.Current.Response.Status = "301 Moved Permanently";
HttpContext.Current.Response.AddHeader("Location",
Request.Url.ToString().ToLower().Replace(
"http://mySite.com",
"http://www.myNewSite.com"));
}
希望能帮助到你
您可以使用此代码永久重定向。@phadaphunk 解决方案将大写字母重定向为小写字母。
string authority = Request.Url.Authority;
if (authority.IndexOf("www.") != 0)
{
Response.StatusCode = 301;
Response.RedirectPermanent("http://www." + authority + Request.Url.AbsolutePath, true);
}
请注意,我认为 Response.RediectPermanent 方法仅适用于.Net 4.0,否则您应该使用 Redirect()