Solved

Working with Session variables == null and response.redirect

Posted on 2013-01-02
7
1,021 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
  • 3
  • 3
7 Comments
 
LVL 34

Accepted Solution

by:
Paul MacDonald earned 400 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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 100 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

679 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