Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Working with Session variables == null and response.redirect

Posted on 2013-01-02
7
Medium Priority
?
1,113 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 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

963 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