用于检测响应重定向的主页

时间:2010-09-11 17:09:51

标签: c# asp.net response.redirect master-pages

每次进行重定向时,我想在我的母版页上清除占位符控件。 如何在代码隐藏中实现这一目标?

我可以检查上次保存的网址和当前网址是否匹配, 但这是一个我不想要的临时解决方案。

像[if(// Page Redirect detected){//做某事}

之类的东西

1 个答案:

答案 0 :(得分:1)

当你进行重定向时,我只是将一个标志转储到会话中。检查母版页中每个加载的标志并清除它,以便后续请求不会不必要地检测到它。也许你可以创建一个重定向助手类来集中设置标志的责任。

if (Session["RedirectFlag"] != null && (bool)Session["RedirectFlag"])
{
    // clear your placeholder
    Session.Remove("RedirectFlag"); // clear the flag
}

...

public static class HttpResponseExtension
{
    public static void RedirectWithFlag(this HttpResponse response, string url)
    {
        response.RedirectWithFlag(url, true);   
    }

    public static void RedirectWithFlag(this HttpResponse response, string url, bool endResponse)
    {
        System.Web.HttpContext.Current.Session["RedirectFlag"] = true;
        response.Redirect(url, endResponse);
    }
}