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开发者_StackOverflow中文版 other approach
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);
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 -
ASP.NET: URI handling
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..
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)
精彩评论