By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,570 Members | 1,325 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,570 IT Pros & Developers. It's quick & easy.

Problems with HTTP_REFERER

P: n/a
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 ?
Nov 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

"Paperback Writer" <ne*********@gmail.com> wrote in message
news:u$**************@TK2MSFTNGP12.phx.gbl...
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 ?

Nov 19 '05 #2

P: n/a
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
"Paperback Writer" <ne*********@gmail.com> wrote in message
news:u$**************@TK2MSFTNGP12.phx.gbl...
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 ?

Nov 19 '05 #3

P: n/a
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" <ne*********@gmail.com> wrote in message
news:u$**************@TK2MSFTNGP12.phx.gbl...
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 ?

Nov 19 '05 #4

P: n/a
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)" <ti*****@despammed.com> escreveu na mensagem
news:e$**************@tk2msftngp13.phx.gbl...
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" <ne*********@gmail.com> wrote in message
news:u$**************@TK2MSFTNGP12.phx.gbl...
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 ?


Nov 19 '05 #5

P: n/a
The fact that you can do this:

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

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

Richard

"Paperback Writer" <ne*********@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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)" <ti*****@despammed.com> escreveu na mensagem
news:e$**************@tk2msftngp13.phx.gbl...
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" <ne*********@gmail.com> wrote in message
news:u$**************@TK2MSFTNGP12.phx.gbl...
Hi, I have the following code in my ASPX:
private string pagina =
System.IO.Path.GetFileName(System.Web.HttpContext. Current.Request.ServerVari
ables["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 ?



Nov 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.