Problems with HTTP_REFERER

P

Paperback Writer

Hi, I have the following code in my ASPX:
private string pagina =
System.IO.Path.GetFileName(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_REFERER"].ToUpper());

The problem is it: When i call this page directly i get an 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.



I really need that, but i do not know how to accomplish this. Does someone
can help me ?
 
K

Kevin Spencer

If there is no HTTP_REFERER (no page from which this page was navigated to),
the ServerVariable will be null. You need to check for this, as "ToUpper()"
will throw an exception if the string is null.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
The sun never sets on
the Kingdom of Heaven
 
M

Mark Fitzpatrick

All it's saying is that there is no value for the HTTP_Referer item, that
means that the array item will be a null value. It's throwing an error
because you cannot convert a null value into an uppercase string, or any
string for that matter.

You have to first check and see if it's null before you attempt to access it

if(Request.ServerVariables["HTTP_REFERER"] != null)
{
// then you can do something
}

If you have to count on the referer information don't, you don't always
receive information so a lot of the times you'll simply have a null value
here.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
J

John Timney \(ASP.NET MVP\)

your referer is probably an empty string if your not referred to the page
from somewhere- your trying to getfilename on an empty string hence the
exception, use a try catch or check the value of referrer first

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director
 
P

Paperback Writer

I can not check with clause IF because it's out of a method...it's a private
string into the class!

John Timney (ASP.NET MVP) said:
your referer is probably an empty string if your not referred to the page
from somewhere- your trying to getfilename on an empty string hence the
exception, use a try catch or check the value of referrer first

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

Paperback Writer said:
Hi, I have the following code in my ASPX:
private string pagina =
System.IO.Path.GetFileName(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_REFERER"].ToUpper());

The problem is it: When i call this page directly i get an 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.



I really need that, but i do not know how to accomplish this. Does
someone can help me ?
 
K

Kan Coon

The fact that you can do this:

System.Web.HttpContext.Current.Request.ServerVariables["HTTP_REFERER"].ToUpp
er());

says you DO have access to the null string in question.

Richard



Paperback Writer said:
I can not check with clause IF because it's out of a method...it's a private
string into the class!

John Timney (ASP.NET MVP) said:
your referer is probably an empty string if your not referred to the page
from somewhere- your trying to getfilename on an empty string hence the
exception, use a try catch or check the value of referrer first

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director
System.IO.Path.GetFileName(System.Web.HttpContext.Current.Request.ServerVari
ables["HTTP_REFERER"].ToUpper());
 

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

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top