In my code, I want to check if a referrer url exists and if the referrer url contains a specific sub string. I know how to check for the sub string:
If( InStr( Request.UrlReferrer.ToString(), "some sub string here" ) > 0 ) Then
But I don’t know how to check if a referrer exists or not. No refer exists if the url is entered in manually in the address bar. So I tried this, but this does not work:
If (Not (Request.UrlReferrer.ToString() = "")) And (InStr(Request.UrlReferrer.ToString(), "some sub string here") > 0) Then
Anyone know how do check if a referrer exists?
I think your problem is you’re using
Andwhen you should be usingAndAlso.Andis NOT short-circuit. So the runtime will evaluate each side of theAndanyway, then returntrueif both sides aretrue. So you’re probably getting aNullReferenceExceptionI guess because you’re trying to look if the referrer contains something, when the referrer is null, because you’ve not used short-circuit evaluation.Conversely,
AndAlsois short-circuit and will not bother to evaluate the second half of the statement if the first half is false.I’ve never yet found a good, compelling reason to use
Andin VB instead ofAndAlso.Anyway this should work for you.