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
Solved

asp.net c# read query string from referering page

Posted on 2010-09-21
7
700 Views
Last Modified: 2013-11-07
Hi

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

Does anybody know how to access the various parts of the query string of the refering page?
0
Comment
Question by:zattz
  • 3
  • 3
7 Comments
 
LVL 10

Expert Comment

by:james-ct16
ID: 33731511
Howdy

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

Regards

James
0
 
LVL 2

Expert Comment

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

Example:
http://www.example.com/index.asp?variable1=45&variable2=34


--index.asp--
<%
Response.Write("<br>" + Request.QueryString("variable1"));
Response.Write("<br>" + Request.QueryString("variable2"));
%>

Open in new window

0
 
LVL 10

Expert Comment

by:james-ct16
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
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:zattz
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"]
0
 
LVL 10

Accepted Solution

by:
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]);
                    }
                }

                System.Diagnostics.Debug.WriteLine(queryString["variable1"]);
            }

Open in new window

0
 

Author Comment

by:zattz
ID: 33731627
           string r = Request.ServerVariables("HTTP_REFERER");
            r = r.Substring(r.IndexOf("?") + 1);
            HttpRequest h = new HttpRequest("", "http://www.site.com", r);
            MessageBox.Show(h.QueryString["XYZ"]);
0
 

Author Comment

by:zattz
ID: 33731787
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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!
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 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