Reading Referrer

T

Thom Little

I am trying to capture the identification of a user from ...
Form request
<a>Tag request
ASP redirection
JavaScript window.location call

I have a simple (I thought) application to capture the referrer. This
application throws an exception stating the "Object reference not an
instance of an object".

Can you tell me how to correct this (apparently obvious) condition?

public class count : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e )
{
string strWork = Request.UrlReferrer.ToString( );
if ( strWork == null )
{
strWork = "Location Undefined" ;
Response.Write( strWork );
}
else
{
strWork = strWork + "?Test=2005" ;
Response.Redirect( strWork );
}
}
 
T

Thom Little

It seems to work when the request comes from an <a> tag.

My assumption is that the UrlReferrer object is only available when the
request comes from a form submission or <a> tag. (Similar to HTTP_REFERRER
in "old" ASP.) Is this a correct assumption?

Is there a means to obtain the URL for the source of EVERY request to the
site?
 
K

Kevin Yu [MSFT]

Hi Thom,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you're getting NullReferenceException
when trying to get UrlReferrer. If there is any misunderstanding, please
feel free to let me know.

As far as I know, UrlReferrer property gets information about the URL of
the client's previous request that linked to the current URL. It is not
only the <a> tag that can be get using the property. Any page with a link
that opens this page can be get from UrlReferrer. For example, we put the
following code in Page_Load, and put a Button control on the page with no
code behind.

private void Page_Load(object sender, System.EventArgs e)
{
if(Request.UrlReferrer != null)
{
Response.Write(Request.UrlReferrer.ToString());
}
}

When the page is load, no URL is displayed. However, when we click the
button, the Url is written on the page.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
T

Thom Little

You have identified the issue from my inadequate description.

On one server I have an aspx page that can be accessed from any page in the
world. When it is accessed I want to identify the address of the calling
page and include the complete address like ...
www.somesomain.com/someindex/somefile.html
.... and if it has a leading http:// or not I don't care.

There are six ways to request my made ...
Manual:
Entered by hand in a browser
Tag:
<a href="http:.://www.tlatla.net/xxxx/test.aspx">Try Test</a>
Form:
<form action="http:.://www.tlatla.net/xxxx/test.aspx"> ... </form>
Script:
<script>window.location = "http:.://www.tlatla.net/xxxx/test.aspx"
;</script>
ASP:
<% response.redirect "http:.://www.tlatla.net/xxxx/test.aspx" %>
ASP.NET:
Response.Redirect( "http:.://www.tlatla.net/xxxx/test.aspx" );

When my page receives control the UrlReferrer is null in all cases except
Tag and Form.

I realize that "Manual" has no meaning in the case and can be ignored. In
all other cases I want to capture the address of the page calling mine.

Can anyone suggest a technique to do this?

Can anyone recommend an article on the subject that would help in my quest?

Thanks for the help.
 
K

Kevin Yu [MSFT]

Hi Thom,

As far as I know, this cannot be done by your application. Because the
UrlReferrer is included in the request, which was sent by the browser. The
only way is to make sure that the client uses Tag or From to link to your
app. Sorry for the inconvenience.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
T

Thom Little

I assume it was done that way for "security" reasons and perhaps it is
appropriate.

I was attempting to develop a "hit counter" on a server that could be called
from pages on other servers. It would be very nice if I could just drop in
a call on the remote server to this server without manually coding in the
return address. This I concluded was not possible and you affirm that it is
not.

The best approach I could find was to have a JavaScript routine construct
the call from the information available and send it to my service on a query
string.

Thanks for the feedback.
 
K

Kevin Yu [MSFT]

Hi Thom,

What you assume, I think, is correct. Thanks for sharing your experience
with all the people here. If you have any questions, please feel free to
post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top