Solved c# read query string from referering page

Posted on 2010-09-21
Last Modified: 2013-11-07

I know you can access parts of the query string in using Request.QueryString["session"]

Does anybody know how to access the various parts of the query string of the refering page?
Question by:zattz
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
LVL 10

Expert Comment

ID: 33731511

To the best of my knowledge that is simply not possible. To achieve this you would have to re transmit from the previous page ie place them in hidden form values or append them to the link address



Expert Comment

ID: 33731513
Request.QueryString returns the variables passed to the webpage via the GET method.


Response.Write("<br>" + Request.QueryString("variable1"));
Response.Write("<br>" + Request.QueryString("variable2"));

Open in new window

LVL 10

Expert Comment

ID: 33731523
however having just said that you could get some of that detail out of Request.ServerVariables("HTTP_REFERER") if it is set by the browser however it is not always so you cant account for it
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.


Author Comment

ID: 33731542
I'm looking for a way to parse the referer so I can access it in a nice way like the request's parameters... ie: Referer.QueryString["session"]
LVL 10

Accepted Solution

james-ct16 earned 500 total points
ID: 33731601
So just to extend my last post further, if the browser does include the refering page then you would have to pick the key pairs out yourself through string manipulation. Code example attached it will need more error checking.

I must stress this is not a reliable method though.

What you should be doing is submitting these values as part of a form or as part of your link.

string refer = Request.ServerVariables("HTTP_REFERER");
            if (!string.IsNullOrEmpty(refer) && refer.Contains("?"))
                refer = refer.Substring(refer.IndexOf("?")+1);
                string[] keyValuePairs = refer.Split('&');
                Dictionary<string, string> queryString = new Dictionary<string, string>();
                foreach (string s in keyValuePairs)
                    string[] pair = s.Split('=');
                    if (pair.Length == 2)
                        queryString.Add(pair[0], pair[1]);


Open in new window


Author Comment

ID: 33731627
           string r = Request.ServerVariables("HTTP_REFERER");
            r = r.Substring(r.IndexOf("?") + 1);
            HttpRequest h = new HttpRequest("", "", r);

Author Comment

ID: 33731787

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In an interesting question ( here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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