Getting the Referrer page or dealing with no referrer page

I have a web page that is supposed to give out a message after a redirect from one of three forms on the site. It works fine as long as it has a referring page, but it bombs if it doesn't or if someone happens to go directly to the page.

public partial class response : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String responseMessage = "";
        
        if (Request.UrlReferrer.ToString().Contains("uploadForm.aspx"))
        {
            responseMessage = "Thank you for sending your file to XXXXXX. An automated email response has been sent to the email address that you specified confirming your successful file upload. We will contact you if we have any questions about your file or any problems opening it. If you do not receive a confirmation email, please contact us at XXXXXX";
        }
        else if (Request.UrlReferrer.ToString().Contains("quoteForm.aspx"))
        {
            responseMessage = "Thank you for sending your quote request to XXXXXX. An automated email response has been sent to the email address that you specified confirming the information you have sent us. We will contact you shortly with a quote or to get more details from you. If you do not receive a confirmation email, please contact us at XXXXXX";
        }
        else if (Request.UrlReferrer.ToString().Contains("contactForm.aspx"))
        {
            responseMessage = "Thank you for sending your message to XXXXX. An automated email response has been sent to the email address that you specified confirming the information you have sent us. We will contact you shortly with an answer to your inquiry. If you do not receive a confirmation email, please contact us at xxxxxx";
        }
        else
        {
            responseMessage = "You have not filled out any of our forms.";
        }
        Label1.Text = responseMessage;
    }
} 

Open in new window


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="response.aspx.cs" Inherits="response" Debug="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="~/styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

Open in new window


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.

Source Error:


Line 11:         String responseMessage = "";
Line 12:        
Line 13:         if (Request.UrlReferrer.ToString().Contains("uploadForm.aspx"))
Line 14:         {
Line 15:             responseMessage = "Thank you for sending your file to XXXXXX. An automated email response has been sent to the email address that you specified confirming your
LVL 9
BobHavertyComhAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dale BurrellDirectorCommented:
if (Request.UrlReferrer != null && Request.UrlReferrer.ToString().Contains("uploadForm.aspx")) {
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BobHavertyComhAuthor Commented:
Hi Dale, thanks.  Your solutions works and I'll give you max points, but I would like to know why it works as I'm missing something in my understanding. I thought that if the Referrer is null, it would just ignore all of the if and else if statements and just execute the final else. Putting a null check seems redundant even though it does work, and I don't understand what it does and why it works.
0
Dale BurrellDirectorCommented:
In dealing with objects you can't call a method on an object if it doesn't exist - you're trying to call ToString() which is a method of the UrlReferrer object, but when there is no referrer this object doesn't exist, and you can't call any methods on it.

This is true of all objects, you have to either be 100% sure they exist, or check whether they exist before making use of them.

Does that help?
0
BobHavertyComhAuthor Commented:
It does make sense now. That's an odd subtlety that I will have to remember. So I guess if it fails the first condition, it does not even check the AND (&&) condition. I would have thought it would check them both but I guess it makes sense for it to stop right there and not even complete the rest of the check. Good subtle detail to know. Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.