Issue with ViewState("URL_Reference")

mike55

Contributor
Joined
Mar 26, 2004
Messages
727
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 can't 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:
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 shouldn't be accessible without a reffere, you should check if the refferer is set.
I just had the same problem, because my Firefox didn't set refferer.
 
Back
Top