Asp.net C# 301 页面过期时重定向
本文关键字:过期 重定向 net Asp | 更新日期: 2023-09-27 18:37:07
我的数据库中有一个到期日期,我想在达到该页面的到期日期时重定向该网页。
我该怎么做?
谢谢
您可以使用页面缓存来执行此操作。我显然不熟悉您如何存储到期日期,但我假设您有 [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()