Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Working with Session variables == null and response.redirect

Posted on 2013-01-02
7
Medium Priority
?
1,089 Views
Last Modified: 2013-01-07
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!
0
Comment
Question by:javierpdx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 34

Accepted Solution

by:
Paul MacDonald earned 1600 total points
ID: 38737605
Modify the code so it doesn't run when someone visits Page.aspx.
0
 
LVL 8

Expert Comment

by:Barry62
ID: 38737688
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.
0
 

Author Comment

by:javierpdx
ID: 38738337
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.
0
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 8

Expert Comment

by:Barry62
ID: 38738365
Is that the entire site.master.cs file?

Also, are you sure you have the CodeBehind reference on every page?
0
 

Author Comment

by:javierpdx
ID: 38738402
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.
0
 
LVL 8

Assisted Solution

by:Barry62
Barry62 earned 400 total points
ID: 38738847
Yes.  The line:

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

is what tells every page to use the master page.
0
 

Author Closing Comment

by:javierpdx
ID: 38752316
Thanks for the help.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question