Help with string.format - creating a url

I am trying to use String.Format to help with creating a url that will contain a parameter from a local variable. I think I'm near, but not sure where to go from here.

Thanks Jason

string link=string.format("<A HREF="http://webserver/?x={0}&y={1}">Click here</A>",variable1,variable2 ) 
+4
source share
1 answer

You need to avoid double quotes:

 string link = string.Format("<A HREF=\"http://webserver/?x={0}&y={1}\">Click here</A>", variable1, variable2); 

If you really want to create the correct HTML with valid URLs, I would recommend the following to you:

 var kvp = HttpUtility.ParseQueryString(string.Empty); kvp["x"] = variable1; kvp["y"] = variable2; var uriBuilder = new UriBuilder("http", "webserver", 80); uriBuilder.Query = kvp.ToString(); var anchor = new TagBuilder("a"); anchor.Attributes["href"] = uriBuilder.ToString(); anchor.SetInnerText("Click here"); string link = anchor.ToString(); 
+7
source

Source: https://habr.com/ru/post/1339732/


All Articles