Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to check if urlreferrer is null

Tags:

c#

asp.net-mvc

I am using the following line of code to check if the UrlReferrer is null

@if (Request.UrlReferrer.AbsolutePath == null)

It just gives me an error of:

System.NullReferenceException: Object reference not set to an instance of an object.

I'm new to asp and have hunted around but can't seem to find anything that will answer my question. The thing that confuses me is if I replace null like so:

@if (Request.UrlReferrer.AbsolutePath == "/Home")

...and the AbsolutePath is indeed /Home, the code works fine, surely I'm asking for the same thing here but with null?

like image 546
Karl Humphries Avatar asked Aug 07 '12 11:08

Karl Humphries


1 Answers

Request.UrlReferrer is null if there is no referrer, which makes your reference to Request.UrlReferrer.AbsolutePath (a property on a null object) throw a null reference exception.

Instead, try;

@if (Request.UrlReferrer == null)
like image 98
Joachim Isaksson Avatar answered Oct 14 '22 16:10

Joachim Isaksson