Problems with HTTP_REFERER

  • Thread starter Thread starter Paperback Writer
  • Start date Start date
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 direcly 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 ?
 
Have you debugged the page to find out which line it is occuring on?
Are you sure this is the line?

If this line is the cause, it is possible that the referrer header was
not sent, in which case, calling that would return null, and then the call
to ToString would throw this exception.

You are better off doing this:

// No need to cast to a string, since it is a NameValueCollection, which
returns strings.
private string pagina =
System.Web.HttpContext.Current.Request.ServerVariables["HTTP_REFERER"];

// Check if null, if not, then continue.
if (pagina != null)
{
// Set the page.
pagina = System.IO.Path.GetFileName(pagina);

// Do the rest of the processing here.
}

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

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 direcly 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 ?
 

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

Back
Top