Nick Kahn
Nick Kahn

Reputation: 20078

How to build a elegant customized URL

i am working on a dashboard page where user will have a multiple choices to select properties and based on the selected properties it will generatea final URL and render.

so let say i have 10 different proprites:

ShowImage=true/false
ShowWindow=true/false
ShowAdmin = true/false
ShowAccounts = true/false
.............
..........
...........

my URL will be static which will be hitting the produciton so there is no change in terms of HOSTNAME.

so here is what i come-up with:

const string URL = "http://www.hostname.com/cont.aspx?id={0}&link={1}&link={2}........"; 
string.Format(URL, "123","aaa123", "123"............); 

but the problem with the above solution is that, regardless it will generate me a long url whether i select or not...

any optimized solution?

Upvotes: 1

Views: 75

Answers (1)

Chad Levy
Chad Levy

Reputation: 10140

You could use the StringBuilder class (System.Text namespace):

StringBuilder sbUrl = new StringBuilder();

sbUrl.AppendFormat("http://www.hostname.com/cont.aspx?id={0}", 123);

if (ShowImage) {
    sbUrl.AppendFormat("&link1={0}", "aaa123");
}

if (ShowWindow) {    
    sbUrl.AppendFormat("&link2={0}", "aaa123");
}

string url = sbUrl.ToString();

Upvotes: 1

Related Questions