[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:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.lblMessage.Text = Page.Request.QueryString("Message").ToString
Dim GotoURL As String
If Not Page.Request.UrlReferrer Is Nothing Then
GotoURL = Page.Request.UrlReferrer.AbsoluteUri
Else
GotoURL = Page.Request.UrlReferrer.ToString <---- Error on this line
End If
Page.Response.AppendHeader("Refresh", "5; url=" & GotoURL)
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.
Re: URLReferrer and Object not set problem
Any ideas? This is VB 2005 for ASP .NET 2.0.
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.
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.
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?
Re: URLReferrer and Object not set problem
Out of interest why are you sending someone back to the page they last came from?
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.
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:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.lblMessage.Text = Page.Request.QueryString("Message").ToString
Dim GotoURL As String
If Not (Page.Request.UrlReferrer Is Nothing) Then
GotoURL = Page.Request.UrlReferrer.AbsoluteUri
End If
Page.Response.AppendHeader("Refresh", "5; url=" & GotoURL)
End Sub
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.
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.
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.
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.
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.
Re: URLReferrer and Object not set problem
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.
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.
Re: URLReferrer and Object not set problem
You're welcome and yep referrer returns a blank string when coming from a secure page.