p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 3.5 Basics (http://p2p.wrox.com/forumdisplay.php?f=351)
-   -   UrlReferrer problem (http://p2p.wrox.com/showthread.php?t=98735)

bhavlice March 19th, 2016 09:31 PM

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.

Imar March 20th, 2016 05:39 AM

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,


bhavlice March 21st, 2016 12:29 PM

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?

Imar March 21st, 2016 03:33 PM

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 March 21st, 2016 03:34 PM

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


bhavlice March 24th, 2016 10:00 AM

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?

Imar March 25th, 2016 07:29 AM

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.



bhavlice March 25th, 2016 11:43 AM

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.

Imar March 28th, 2016 09:29 AM

>> 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">....


bhavlice March 28th, 2016 03:45 PM

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?

Imar March 29th, 2016 12:19 PM

Yep, like I said, there could be all kinds of reasons it's not there, including security software running on the browser's machine that strips it off.

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

There wasn't any code I was referencing. I was just suggesting to change your current links to include the original page names. Then on the target site you can grab the URL from the query string.

Does that help?


bhavlice March 29th, 2016 12:25 PM

UrlReferrer problem
Once again, thanks for all your help. I will try working with this and let you know the outcome, if any.

All times are GMT -4. The time now is 12:15 PM.

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