firebird
firebird

Reputation: 3521

HttpContext.Current is null for bots

I have the following code in a class which is called by a .NET page (asp.net webforms during page_load event):

public static bool BrowserSupportsJS
{
   get { return (HttpContext.Current.Session["js_support"] != null 
   && ((bool)HttpContext.Current.Session["js_support"]));
}

This line throws an exception for any bot: googlebot, bingbot etc

The exception is: Object reference not set to an instance of an object and it is on the get accessor line. It looks like HttpContext.Current is null.

Upvotes: 1

Views: 558

Answers (1)

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 121017

You should check the Session for null like so:

public static bool BrowserSupportsJS
{
   get 
   { 
       if(HttpContext.Current.Session == null)
           return false;
       return (HttpContext.Current.Session["js_support"] != null 
           && ((bool)HttpContext.Current.Session["js_support"]));

   }
}

Upvotes: 3

Related Questions