当页面过期时,Asp.net c#301重定向

时间:2012-06-13 15:32:15

标签: c# asp.net sql webforms web-config

我的数据库中有一个到期日期,我想在达到该页面的到期日期后重定向网页。

我该怎么做?

由于

3 个答案:

答案 0 :(得分:1)

您可以使用网页缓存来执行此操作。我显然不熟悉你如何存储过期日期,但我认为你有[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,以便在修改数据库中的过期日期时更新它...

答案 1 :(得分:0)

您可以在您的数据库上放置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"));
          }

希望有所帮助

答案 2 :(得分:0)

您可以使用此代码永久重定向。 @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()

相关问题