noisecoder
noisecoder

Reputation: 109

Recursive redirect on error handler page

I use the following MSDN code on my master page to detect if the browser accepts cookies:

protected void Page_Load(object sender, EventArgs e)
{
    if(!this.IsPostBack) {
        if(Request.QueryString["CookieTest"] == null) {
            Response.Cookies["TestCookie"].Value = "Test";
            Response.Redirect("CookieCheck.aspx.redirect=" + Server.UrlEncode(Request.Url.ToString())));
        }
        else if((string)Request.QueryString["Test"] == "passed") {
            // my stuff...
        }
    }
}

The CookieCheck.aspx contains the following:

protected void Page_Load(object sender, EventArgs e)
{
    if(Request.Cookies["TestCookie"] == null)
        Response.Redirect(Request.QueryString["redirect"] + "?Test=notPassed", true);
     else
        Response.Redirect(Request.QueryString["redirect"] + "?Test=passed", true);
}

Within the web.config i have defined the following:

<customErrors mode="On" defaultRedirect="Error.aspx" />

Now the recognizing of the cookies works well, but I have this problem: Whenever an error occurs on the page and I should be redirected to Error.aspx (and this worked before the whole cookie detection thing), the redirection seems stuck in an infinite loop and appends more and more "?Test=passed" to the URL. I should mention that the Errors.aspx also has the same masterpage and thus also performs the cookie check. However I have no clue why the redirection doesn't stop. Is there a way to solve this problem other than to exlude the Errors.aspx page from having the master page? Thank you very much.

Upvotes: 0

Views: 1297

Answers (2)

bashmohandes
bashmohandes

Reputation: 2376

If the CookieCheck.aspx page also uses the same Master page it will keep redirecting recursively, make sure that CookieCheck.aspx is not using the same MasterPage.

I'd rather recommend not using MasterPages for this, Master Pages by design are for Visual Inheritance not code Inheritance, if you wish to make some special type of pages that checks for the the browser ability to use cookies, you can have a new base class for these pages

public abstract class CookieEnabledPage : Page
{
}

and add your logic to this class, then whenever you need to make a new page with this behavior you inherit from this base class. I think this is a much cleaner way of doing what you want.

Upvotes: 1

Utaal
Utaal

Reputation: 8534

I guess the masterpage (or the combination masterpage-error.aspx) is raising an exception which triggers a redirect to error.aspx, which in turn causes the masterpage to restart its lifecycle and raise a new exception. The concatenation of "?Test=passed" is almost certainly a side effect of reinvoking the cookie test every time an error redirect occurs. I suggest firing up the debugger and setting a breakpoint at Page_Load in Masterpage.aspx.cs and step through until you are redirected to the error page (the last line which gets execued is the one raising the exception).

Upvotes: 0

Related Questions