Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 19th, 2016, 09:31 PM
Registered User
 
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.
 
Old March 20th, 2016, 05:39 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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!
 
Old March 21st, 2016, 12:29 PM
Registered User
 
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?
 
Old March 21st, 2016, 03:33 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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!
 
Old March 21st, 2016, 03:34 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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!
 
Old March 24th, 2016, 10:00 AM
Registered User
 
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?
 
Old March 25th, 2016, 07:29 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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!
 
Old March 25th, 2016, 11:43 AM
Registered User
 
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.
 
Old March 28th, 2016, 09:29 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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!
 
Old March 28th, 2016, 03:45 PM
Registered User
 
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?





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.