I'm trying to reconcile a few opinions I've found out in the wild. One source says that in C# Request.UrlReferrer will throw a System.UriFormatException if the HTTP header is malformed. The other says that Request.UrlReferrer actually doesn't throw an exception in this case, it just returns null.
That said, I'm developing a webapp that will capture the Referral Url for future reference. I'm checking it for null so that the "not set to instance of an object" error does not burst forth when converting to a string, etc., and that tests out fine. I am also wondering whether I should include error handling for potential System.UriFormatExceptions.
Has anyone run into this? Thank you!
EDIT: Given NightOwl888's answer below, it does appear that exceptions can occur. How often, I'm not sure, but I'd rather protect against it than risk exceptions on a public site.
I am only grabbing the referralUrl for logging, so it won't get used in any downstream application context. It's just getting grannGiven that, I'm guessing that the following code should cover me:
Uri referrer = null;
try
{
referrer = Request.UrlReferrer;
}
catch (UriFormatException)
{
referrer = null;
}
catch (Exception)
{
referrer = null;
}
var referralUrl = (referrer != null) ? referrer.ToString() : "None Found";
EDIT: Changed exceptions and added general catch