Results 1 to 17 of 17

Thread: [RESOLVED] URLReferrer and Object not set problem

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Resolved [RESOLVED] URLReferrer and Object not set problem

    Hello,

    I am creating a message page that displays a message, then returns the user to the referrer, but I am having a problem.

    The error I get is that an object reference is not set. Is there a problem getting the referrer in the page load event?

    Here is my code.

    VB Code:
    1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    2.  
    3.         Me.lblMessage.Text = Page.Request.QueryString("Message").ToString
    4.  
    5.         Dim GotoURL As String
    6.  
    7.         If Not Page.Request.UrlReferrer Is Nothing Then
    8.             GotoURL = Page.Request.UrlReferrer.AbsoluteUri
    9.         Else
    10.             GotoURL = Page.Request.UrlReferrer.ToString <---- Error on this line
    11.         End If
    12.  
    13.         Page.Response.AppendHeader("Refresh", "5; url=" & GotoURL)
    14.  
    15.     End Sub

    I understand the error(I think), but I am not sure how that applies in this situation, or how to fix it. I should also mention that I am passing a variable thru the querystring to this page. Would that affect it?

    BTW, I found this code on this site while I was searching for a solution. I originally just had the Page.Response.UrlReferrer.ToString.
    Last edited by Krenshau; Nov 17th, 2006 at 04:24 PM.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Re: URLReferrer and Object not set problem

    Any ideas? This is VB 2005 for ASP .NET 2.0.

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: URLReferrer and Object not set problem

    The Else code will execute if the UrlReferrer IS Nothing. You can't call a method of something that doesn't exist.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Re: URLReferrer and Object not set problem

    Ok, thank you brucevde. That is one of those "DUH" things on my part. I should have been able to put that together. However, that still leaves me with the other question. Can I get the URLReferrer in the load event? Basically, you probably can tell what I am trying to do, but is it possible to append the header with the URLReferrer in the page load event, or should I be doing this differently? If differently, how?

    Thank you for your help.

  5. #5
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Re: URLReferrer and Object not set problem

    In which case your code looks fine if you just remove the else part.

    So the question is what do you want to happen if there is no referrer?

  6. #6
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Re: URLReferrer and Object not set problem

    Out of interest why are you sending someone back to the page they last came from?

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Re: URLReferrer and Object not set problem

    But, there is a referrer. If I pass a variable thru the query string to the message page, there has to be a referrer, else there would be no message to display, and the page wouldn't be called.

    I have a profile page that allows the user to edit their profile. If there is a problem, then the user is sent to the message page, which will display a message passed from the previous page thru the query string. In the page load event I pull the message from the URL, display it in the label, then append the header to refresh back to the previous page after five seconds.

    Am I misunderstanding how the referrer works here? I thought I understood it, but I am fairly new to .NET and ASP .NET.

    Thank you for your help.

  8. #8
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Re: URLReferrer and Object not set problem

    Ok so in your case you will always have a refferer.

    So if your code now looks like below it should work.
    VB Code:
    1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    2.  
    3.         Me.lblMessage.Text = Page.Request.QueryString("Message").ToString
    4.  
    5.         Dim GotoURL As String
    6.  
    7.         If Not (Page.Request.UrlReferrer Is Nothing) Then
    8.             GotoURL = Page.Request.UrlReferrer.AbsoluteUri          
    9.         End If
    10.  
    11.         Page.Response.AppendHeader("Refresh", "5; url=" & GotoURL)      
    12.  
    13.     End Sub
    Last edited by Fishcake; Nov 19th, 2006 at 06:58 PM.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Re: URLReferrer and Object not set problem

    That worked, but I don't understand why.

    What is teh difference between:

    GotoURL = Page.Request.UrlReferrer.ToString

    and

    GotoURL = Page.Request.UrlReferrer.AbsoluteUri

    Thank you for your help.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Re: URLReferrer and Object not set problem

    I spoke to soon, it took me to the message page, which is correct, but now it just refreshes the same page, it doesn't take me back to the previous page.

  11. #11
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Re: URLReferrer and Object not set problem

    Quote Originally Posted by Krenshau
    That worked, but I don't understand why.

    What is teh difference between:

    GotoURL = Page.Request.UrlReferrer.ToString

    and

    GotoURL = Page.Request.UrlReferrer.AbsoluteUri

    Thank you for your help.
    In terms of what they return there is no difference.

  12. #12
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Re: URLReferrer and Object not set problem

    Quote Originally Posted by Krenshau
    I spoke to soon, it took me to the message page, which is correct, but now it just refreshes the same page, it doesn't take me back to the previous page.
    I presume that's because GotoURL isn't getting set so it's refreshing the same page.

    Have you tried stepping through your code? Can you post your code as it should work.

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Re: URLReferrer and Object not set problem

    Yep, that is it. I put another label on the page and if it went into the if statement it would put GotoURL in the label, but it is just blank.

    I am not sure what you mean about posting my code as it should work. The code in the message page was changed to what you posted, I am deliberatly creating errors in my profile page so I can test this message page.

    I did try changing the Page.Request.UrlReferrer.AbsoluteUri to Page.Request.UrlReferrer.ToString at first because I thought it might have been the problem, but other than that I am not sure what do do since it thinks the referrer is nothing.

  14. #14
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: URLReferrer and Object not set problem

    I found this on DevX that might help you...

    http://www.devx.com/vb2themax/Tip/18...ticle&trk=MSCP

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Re: URLReferrer and Object not set problem

    I think I found what my problem is, and what I am doing won't work according to this.

    http://msdn.microsoft.com/library/de...s/referrer.asp

    My site is using SSL, which, if I understand that article correctly, referrers don't work in SSL. So, I guess I will have to find a different way. I am doing some checking on it now, but if anyone has any suggestions as to how I can do this, I would be grateful.

    Thank you for the help.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Re: URLReferrer and Object not set problem

    Nevermind, I don't know why I didn't think of it earlier. I got it to work with session variables.

    Thank you all very much for your help.

  17. #17
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Re: URLReferrer and Object not set problem

    You're welcome and yep referrer returns a blank string when coming from a secure page.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width