Issue with ViewState("URL_Reference")

mike55

Well-known member
Joined
Mar 26, 2004
Messages
726
Location
Ireland
Hi all

I am using the following cmd in the post-back section of my page to identify what page I came from. Here is the code that I am using:
Code:
If Not Page.IsPostBack Then
            ViewState("ReferenceURL") = Request.UrlReferrer.ToString
end if

I am however getting the following error: "Object reference not set to an instance of an object". I cant understand what is causing the error, as when the page is called from another page it works perfectly. Any suggestions? I have a sample application using the smae code and there is no problem.

Could it be anything to do with the fact that I am using the asp.net 2.0 menu control to redirect the user?

Mike55.
 
Last edited by a moderator:
You would just need to check if Request.UrlReferer is nothing before trying to convert it to a string, if you havent been referred then it wont be set.
 
My question then is, why does the Request.UrlReferer have a value some times and not other times?

Mike55.
 
The UrlReferer is only valid or will have value when coming from or being "referred" by another page.

So if a visitor hits your site from a desktop shortcut, favorites item, or typing in the url in the address bar, or anything of that nature there will be no referer to track in session.

Now if you click on a link from another page ... then that page will become the referer page.

If you have issues with it throwing exceptions I stop it by having a global static class function in most apps to take care of it like this ....

public static String QString(String paramName)
{
String tmpS = String.Empty;

try
{
tmpS = HttpContext.Current.Request.ServerVariables[paramName].ToString();
}
catch
{
tmpS = String.Empty;
}

return tmpS;
}

Hope this helps ....
 
In addition, some browsers or other settings may disallow HTTP refferes. So even when the page shouldnt be accessible without a reffere, you should check if the refferer is set.
I just had the same problem, because my Firefox didnt set refferer.
 
Back
Top