ASP.NET 2.0 - Cleaner approach to handle QueryString
Well there are a number of ways to handle your QueryString. So what is a QueryString?
Request.QueryString["fundid"]
This is how you use a QueryString in your web application? Basically you should not store too many data in your QueryString. Furthermore one of the benefits of QueryString, you can bookmark it. Nice right. However, you should not abuse it too much.
So what is the problem with the top approach? Well it is not wrong.
Let's go in detail.
What is if you want to check whether that QueryString is null or empty string?
This is what a lot of developers will do?
if (Request.QueryString["fundid"] != null && Request.QueryString["fundid"].ToString() == string.Empty) {}
3 reasons not to do that way.
a) Why makes this so complicated? Do this instead:
if (!string.IsNullOrEmpty(Request.QueryString["fundid"])) {}
2 in 1 method. Okay, not many developers aware of this. Surprised to see from my developers today.
Simple right.
b) I prefer not to handle all this in the user control or web form. I will prefer to create a class probably AppQueryString to handle all this dirty plumbing for me.
c) What if you misspell fundid to FundID or type the wrong name? This happens a lot of time.
Other than these 3 reasons, there is one more headache. What if you cast the wrong data type? You will get a runtime error. Assuming in fundid QueryString, you are suppose to get decimal but you cast into byte.
So what is a cleaner approach for this?
I will introduce this way which I feel is more suitable for me to handle the QueryString. Again this is my personal point of view.
public static class AppQueryString
{
#region Private Constants
private const string fundID = "fundid";
private const string fundFlag = "nuflag";
#endregion
#region Public Properties
public static decimal FundID
{
get
{
decimal result;
// Check if is null or empty
if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[fundID]))
{
// Try casting and make sure the data type is correct
if (decimal.TryParse(HttpContext.Current.Request.QueryString[fundID], out result))
{
return result;
}
}
return default(decimal);
}
set
{
HttpContext.Current.Request.QueryString[fundID] = value.ToString();
}
}
public static bool FundFlag
{
get
{
byte result;
// Check if is null or empty
// Check if the length == 1
if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[fundFlag]) && HttpContext.Current.Request.QueryString[fundFlag].Length == 1)
{
// Try casting and make sure the data type is correct
if (byte.TryParse(HttpContext.Current.Request.QueryString[fundFlag], out result))
{
// Check if result == 1
if (result == 1)
{
return true;
}
}
}
return false;
}
set
{
HttpContext.Current.Request.QueryString[fundFlag] = value.ToString();
}
}
#endregion
}
So when you use this QueryString, you don't have to care about casting it yourself. Let the class handles all the dirty plumbing for you.
a. You will not face any casting issues.
b. Focus on your QueryString validations in this class. So your presentation layers look cleaner.
c. You can even use Microsoft AntiXss 1.5 to validate the QueryString.
Lastly, you can apply this approach to Session and Viewstate. However there is something you should consider especially on Session - ObjectDataSource.SelectParameters. Basically when you use ObjectDataSource you can have a SessionParameter for input.
You cannot pass in maybe SessionName.SESSION_MODULEA into the SessionParameter.SessionField. It will not get any value. In order to solve this, you just have to programmatically pass the session like this:
ObjectDataSourceInstance.SelectParameters.Add("fundID", TypeCode.Decimal, Session[SessionName.SESSION_MODULEA].ToString());
I had tried to use SessionParameter programmatically but I kept getting an IConvertible error.
This is the only way I can see how to get it work? Of course people hate to do things programmatically :( If you know a better way, let me know. Thanks.