Link to home
Start Free TrialLog in
Avatar of javierpdx
javierpdxFlag for United States of America

asked on

Working with Session variables == null and response.redirect

I'm getting an error when trying to add code in my Site.Master page.
Error: The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
This problem can sometimes be caused by disabling or refusing to accept cookies.

When I use this code in the actual page (not the Site.Master) I don't get an error and it redirects.  When I try  to add this to my site.master, I get the above error in Firefox.  I thought if I added it to the site master it would always check for the session ID and redirect if it was not found.

Code I am using in
            if (Session["ID"] == null)
            {
                Response.Redirect("page.aspx");  //this page will have end user set the ID session variable before continuing.  
            }
            else
            {
                //stay on page
            }

Is there a better way to handle this?    I was trying to avoid having to add the above code to every page that checks for a Session["ID"].

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Paul MacDonald
Paul MacDonald
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Could we see the entire site.master code?  Also, are you sure your master page and your 'actual' page have the exact same code?  You can change them independently.
Avatar of javierpdx

ASKER

The site.master does not load the page load on each link click.  My code below only works on initial load.
When I add  "if (url.EndsWith("page.aspx"))" it ignores my link clicks.

This is the code I'm using.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Portal
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Changes all users to HTTPS
            string url = HttpContext.Current.Request.Url.ToString();
            if (url.StartsWith("http:") && !url.Contains("localhost"))
            {
                HttpContext.Current.Response.Redirect("https" + url.Remove(0, 4), false);
            }
          
           if (url.EndsWith("NSPLogin.aspx"))
                {
                    //ignores "else"
                }

                else
                {
                    if (Session["ID"] == null)
                    {
                        Response.Redirect("Page.aspx");
                    }
                    else
                    {
                        //stay on page
                    }
                }           
        }
    }
}

Open in new window

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="Portal.SiteMaster" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
    <title></title>
    <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
    <!-- favorites ico -->
<link rel="shortcut icon" href="Images/favicon.ico" />
    <asp:ContentPlaceHolder ID="HeadContent" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form runat="server">
    <div class="page">
        <div class="header">
            <div class="title">
                <h1>
                    Portal
                </h1>
            </div>
            <div class="clear hideSkiplink">
                <asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
                    <Items>
                        <asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home"/>
                        <asp:MenuItem NavigateUrl="~/AddDetails.aspx" Text="Add Student Application"/>
                        <asp:MenuItem NavigateUrl="~/Counts.aspx" Text="View Reports" 
                            Value="View Reports"/>
                    </Items>
                </asp:Menu>
                
            </div>
        </div>
        <div class="main">
            <asp:ContentPlaceHolder ID="MainContent" runat="server"/>
        </div>
        <div class="clear">
        </div>
    </div>
    <div class="footer">
    </div>
    </form>
</body>
</html>

Open in new window

Thanks.
Is that the entire site.master.cs file?

Also, are you sure you have the CodeBehind reference on every page?
That is the entire file.

No.   I thought I could avoid adding the reference on every page.    That is why I was using the site.master.cs for the reference.   Do I need to add it to every page?

Thanks.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the help.