データベースに有効期限があり、そのページの有効期限に達したときに Web ページをリダイレクトしたいと考えています。
どうすればいいですか?
ありがとう
データベースに有効期限があり、そのページの有効期限に達したときに Web ページをリダイレクトしたいと考えています。
どうすればいいですか?
ありがとう
ページのキャッシュを使用してそれを行うことができます。有効期限をどのように保存しているかはわかりませんが、[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() を使用する必要があります