Always check for null on Request.UserAgent…

Personally, I love the “Browser Capabilities” project on codeplex (http://browserdetection.codeplex.com/) that does a nice job of extracting the actual things that a browser can do, saving you from doing things the following:

if (Request.UserAgent.IndexOf("AppleWebKit") > 0)
{
//DO SOMETHING
}

But if you find yourself in a situation where you have custom logic you need to implement around a piece of logic (like I did with a mobile site I recently worked on), rememeber to always do a null check on the UserAgent before performing string operations on it.

While you’re at it, please don’t do these things either (I won’t say why not – if you don’t know why not, ask somebody):

Request.QueryString["page"].ToString();
DataRow["column"].ToString();

 

More later - joel

Published Monday, September 14, 2009 9:50 AM by joelvarty
Filed under: ,

Comments

# re: Always check for null on Request.UserAgent…

Wednesday, October 07, 2009 3:23 PM by Shawn

"I won't say why not"

Because that would be helpful?

# re: Always check for null on Request.UserAgent…

Wednesday, October 07, 2009 3:50 PM by joelvarty

Don't do a ToString() on an object returned from a collection or indexer because it might be null...

Leave a Comment

(required) 
(required) 
(optional)
(required)