i have 6-7 bool values like bookmark, authoredBy and some others as filters i want to form url such as if any filter applied it concatenate to the main url like something?bookmark=true&author=true and like this i can use if else but that will be lengthy are there any other approach
Asked
Active
Viewed 115 times
4 Answers
1
Well, do this way ,
........
string url = "http://www.google.com?";
if(bookmark)
{
url+="bookmark=true&"
}
if(author)
{
url+="author=true&"
}
...........
in the same way for other variables..

Srinivas Reddy Thatiparthy
- 10,985
- 4
- 45
- 68
-
what if multiple filters are applied simultaneously – Mac Nov 29 '10 at 09:53
-
suppose along with bookmark author filter is also applied then the Querystring should like something?bookmark=true&author=true like this if all six filters are there then add all six values to query string – Mac Nov 29 '10 at 10:00
-
@Mac do that if condition for author also,like i did for bookmark. – Srinivas Reddy Thatiparthy Nov 29 '10 at 10:02
-
that is what i am doing but the code becomes a bit lengthy so i was wondering if there r any other simple approach any ways thanks – Mac Nov 29 '10 at 10:04
-
@Mac Then use Dictionary
and set the keys and iterate through it – Srinivas Reddy Thatiparthy Nov 29 '10 at 10:08 -
@Mac, simple way is using ternary operators. – RameshVel Nov 29 '10 at 10:08
-
@Srinivas solution looks good, however, as @Pabuc has listed, putting the suffix "&" at the end is better. If nothing after, no harm, but if her "bookmark" is not used, your URL will be ..google.com&author=true and fail. – DRapp Nov 29 '10 at 11:08
1
String url = "google.com?";
bool bookmark = true, author=true;
if(bookmark)
{
url += "bookmark=true&";
}
if(author)
{
url = "author=true&"
}
url = url.Substring(0,url.length-1);

Pabuc
- 5,528
- 7
- 37
- 52
-
Srinivas's way wouldn't work if bookmark is false..The url would be google.com?&author=true which isn't right. You should add as a suffix not prefix. My way is the best way and won't fail ;) – Pabuc Nov 29 '10 at 10:14
1
Consider using the URIBuilder
class. Check this example - http://www.codeproject.com/KB/aspnet/UrlBuilder.aspx
Alternatively, check the answer to this SO question. This is a more object oriented solution -

Community
- 1
- 1

Unmesh Kondolikar
- 9,256
- 4
- 38
- 51
0
string formQueryFromBool(string name , bool val){
return ((val) ? name + "=" + val.toString() : "" );
}
bool bookmark=false;
bool author=true;
var qString = formQueryFromBool("bookmark",bookmark) + "&" + formQueryFromBool("author",author)

RameshVel
- 64,778
- 30
- 169
- 213
-
I don't think that is what he means. He probably want to know how to receive it clean on the server side. – Filip Ekberg Nov 29 '10 at 09:48
-
ya you are right filip what i want if any value is false then don't add it to querystring add only those which are true – Mac Nov 29 '10 at 09:50
-
@Mac & @Filip Ekberg & @Srinivas, i have upadated this before anyone comments, i think you have all posted when am updating my post........ – RameshVel Nov 29 '10 at 09:54
-
@Srinivas, readability doesnt matter, OP wants to avoid lengthy if...else loop. Then this is the simple way... – RameshVel Nov 29 '10 at 10:06