tags:

views:

242

answers:

3

Hey,

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.

A: 

does the first "Test=notPassed" querystring value ever get removed from the redirect url?

John Weldon
No it doesn't, neither does "?Test=passed". Does this indicate a problem or bad pratice? Because here http://msdn.microsoft.com/en-us/library/ms178194.aspx at the very bottom they also don't seem to remove it.
noisecoder
John Weldon
+1  A: 

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.

bashmohandes
No, the CookieCheck.aspx doesn't have a master page, so that's not the reason.Your approach with the new base class seems very clean and reasonable, I'll consider this one.
noisecoder
A: 

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).

Utaal
I just checked this one, and I don't think that's the reason. It's more like it constantly switches between error page's page_load, masterpage's page_load and the cookietest page without any reason whatsoever.
noisecoder