Go Back   Velocity Reviews > Newsgroups > ASP Net
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

ASP Net - Problems with HTTP_REFERER

 
Thread Tools Search this Thread
Old 07-19-2005, 10:02 PM   #1
Default Problems with HTTP_REFERER


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 ?




Paperback Writer
  Reply With Quote
Old 07-19-2005, 10:14 PM   #2
Kevin Spencer
 
Posts: n/a
Default Re: Problems with HTTP_REFERER
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" <> wrote in message
news:u$...
> 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 ?
>
>





Kevin Spencer
  Reply With Quote
Old 07-19-2005, 10:17 PM   #3
Mark Fitzpatrick
 
Posts: n/a
Default Re: Problems with HTTP_REFERER
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" <> wrote in message
news:u$...
> 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 ?
>
>





Mark Fitzpatrick
  Reply With Quote
Old 07-19-2005, 10:19 PM   #4
John Timney \(ASP.NET MVP\)
 
Posts: n/a
Default Re: Problems with HTTP_REFERER
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" <> wrote in message
news:u$...
> 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 ?
>
>





John Timney \(ASP.NET MVP\)
  Reply With Quote
Old 07-19-2005, 10:28 PM   #5
Paperback Writer
 
Posts: n/a
Default Re: Problems with HTTP_REFERER
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)" <> escreveu na mensagem
news:e$...
> 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" <> wrote in message
> news:u$...
>> 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 ?
>>
>>

>
>





Paperback Writer
  Reply With Quote
Old 07-20-2005, 07:59 AM   #6
Kan Coon
 
Posts: n/a
Default Re: Problems with HTTP_REFERER
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" <> wrote in message
news:%...
> 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)" <> escreveu na mensagem
> news:e$...
> > 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" <> wrote in message
> > news:u$...
> >> 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 ?
> >>
> >>

> >
> >

>
>





Kan Coon
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Pioneer DVR-105 and Nero Problems grams@oldtown.com DVD Video 1 08-12-2003 04:17 AM
Re: Pioneer DVR-105 and Nero Problems Ron DVD Video 1 08-08-2003 06:33 PM
Re: Pioneer DVR-105 and Nero Problems Flossie DVD Video 0 08-07-2003 02:25 PM
Re: Pioneer DVR-105 and Nero Problems Flossie DVD Video 0 08-07-2003 08:11 AM
Re: Pioneer DVR-105 and Nero Problems CAM DVD Video 0 08-07-2003 02:30 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46