Trying to check if urlreferrer is null

I use the following line of code to check if there is UrlReferrer null

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

This just gives me an error:

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

I am new to asp and have been hunting around, but cannot find anything that answers my question. What bothers me is to replace zero like this:

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

... and AbsolutePath really / Home, the code works fine, of course, I ask about the same here, but with null?

+5
source share
2 answers

Request.UrlReferrer is null if there is no referrer that refers to Request.UrlReferrer.AbsolutePath (a property on a null object) throws a null exception exception.

Try instead

@if (Request.UrlReferrer == null)
+10
source

, :

var absoluteUrl = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
string returnUrl = absoluteUrl.Replace(System.Web.HttpContext.Current.Request.Url.PathAndQuery, "/");
0

All Articles