Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 19th, 2016, 10:31 PM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2016
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default UrlReferrer problem

I'm using the following simple code in VB.NET:

Dim MyUrl As Uri = Request.UrlReferrer
Response.Write("Referrer URL Port: " & Server.HtmlEncode(MyUrl.Port.ToString()) & "<br>")
Response.Write("Referrer URL Protocol: " & Server.HtmlEncode(MyUrl.Scheme) & "<br>")

I am receiving the following error:


Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 54:
Line 55: Dim MyUrl As Uri = Request.UrlReferrer
Line 56: Response.Write("Referrer URL Port: " & Server.HtmlEncode(MyUrl.Port.ToString()) & "<br>")
Line 57: Response.Write("Referrer URL Protocol: " & Server.HtmlEncode(MyUrl.Scheme) & "<br>")

This used to work but for some reason it no longer does and I don't know why.
  #2 (permalink)  
Old March 20th, 2016, 06:39 AM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,955
Thanks: 79
Thanked 1,556 Times in 1,533 Posts
Default

Hi there,

The referrer is not guaranteed to be there. It won't be there when you browse to a page directly or when you have local software removing it fore example. Before you write out the port and other info, check if Request.UrlReferrer is not Nothing.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #3 (permalink)  
Old March 21st, 2016, 01:29 PM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2016
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default UrlReferrer

Thanks for the response. Yes in deed, you are right, the Referrer port is nothing. However, it is definitely coming from another page with a URL reference. so I'm still not at all clear why it does not show up in the Request. Any ideas why that would be?
  #4 (permalink)  
Old March 21st, 2016, 04:33 PM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,955
Thanks: 79
Thanked 1,556 Times in 1,533 Posts
Default

Like I said, it could be stripped away by security software. Also, when you're using SSL you won't see the referrer.

In general, don't count on the referrer being there; always assume it could be null / Nothing.

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #5 (permalink)  
Old March 21st, 2016, 04:34 PM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,955
Thanks: 79
Thanked 1,556 Times in 1,533 Posts
Default

Or are you saying the URL is there but not the pot? Could it be the default port?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #6 (permalink)  
Old March 24th, 2016, 11:00 AM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2016
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default UrlReferrer response

Thanks very much for all your help. I'm not sure I can count on any referrer being there which is not good. This function used to provide me with the ability to return to the page that sent the functional page after the processing is completed. This is particularly useful for Mobile sites to eliminate backing up to previous pages on a close. I guess with all the recent security concerns this request function is no longer a viable option. What a shame. Does anyone know of an alternative way to return to the referrer?
  #7 (permalink)  
Old March 25th, 2016, 08:29 AM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,955
Thanks: 79
Thanked 1,556 Times in 1,533 Posts
Default

I don't think there's an alternative if you don't control the previous page / site. If the browser doesn't send the referrer, or it gets stripped from the request by software, you'll never see it. If you do control the previous site you could send it as a request parameter.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #8 (permalink)  
Old March 25th, 2016, 12:43 PM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2016
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default UrlReferrer response

Once again, thank you so much for responding to my question. Your input has been invaluable to me. I've been at this for more years than I care to remember, but I'm still learning.

I do normally control the entire site where the requests are coming from which is why I was surprised that the referrer stopped working. How can I send it as a request parameter? I thought it was included automatically. However, if I use: Request.ServerVariables("ALL_HTTP"), no UrlReferrer shows up.
  #9 (permalink)  
Old March 28th, 2016, 10:29 AM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,955
Thanks: 79
Thanked 1,556 Times in 1,533 Posts
Default

>> How can I send it as a request parameter? I thought it was included automatically.

It will be, when it's there. But if the browser doesn't send it, it won't be there.

I was thinking of something like thsi:

<a href="http://www.secondsite.com?From=http://www.firstsite.com/somepage.aspx">....

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #10 (permalink)  
Old March 28th, 2016, 04:45 PM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2016
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default UrlReferrer problem

It is definitely not there. Is it possible that the latest IE version 11 has something to do with blocking the UrlReferrer? It seems that the last time this worked for me was on an earlier version but unfortunately, IE is, as you know, the most commonly used.

Is there some way you can send me the sample code you referenced?
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Request.urlreferrer property rajatake ASP.NET 2.0 Professional 1 October 4th, 2007 04:40 AM
Request.UrlReferrer.ToString() mike72 BOOK: ASP.NET Website Programming Problem-Design-Solution 2 September 5th, 2005 10:31 AM
ASP.NET Website Programming & Request.UrlReferrer mike72 Wrox Book Feedback 0 September 3rd, 2005 08:16 PM
Problem with Request.UrlReferrer.ToString velateam .NET Framework 2.0 0 April 5th, 2005 09:22 AM



All times are GMT -4. The time now is 10:44 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.