I speedily wrote a bunch of crappy checks, to check whether strings are empty, in order to build a query string.
StringBuilder SB = new StringBuilder();
SB.AppendFormat("{0}guide/search.aspx?", root);
root is a const, containing the website address.
It's like this because we have test servers.
if (!String.IsNullOrEmpty(item)) {
SB.AppendFormat("&i={0}", Server.UrlEncode(item));
}
if (!String.IsNullOrEmpty(found)) {
SB.AppendFormat("&f={0}", Server.UrlEncode(found));
}
if (!String.IsNullOrEmpty(what)) {
SB.AppendFormat("&wt={0}", Server.UrlEncode(what));
}
if (!String.IsNullOrEmpty(where)) {
SB.AppendFormat("&wr={0}", Server.UrlEncode(where));
}
I tried replacing these with ternaries, but I couldn't get my head around how to do that with the sb.AppendFormat in place.
- Is there a better way to check empty strings than -
String.IsNullOrEmpty? - Is my method of naming variables correct with C# standards?
- Is
Server.UrlEncodethe best way to format strings into query strings?