Silverlight InitParams, where the value has a comma

In Silverlight 2, using C # on ASP.NET, you can pass a set of initialization parameters by assigning a line to the Silverlight InitParams object containing a pair of key / value pairs, separated by commas.

I have seen other systems that have a similar mechanism for transmitting collections of key / value pairs as a single line.

What is a solution to indicate a value that has a comma in it?

For example, this line has no problem:

string s1 = "key1=value1,key2=value2"; 

but it does:

 string s2 = "key1=value1,key2=two,values"; 

i.e. "two values" must have some comma delay ...

+4
source share
2 answers

Unfortunately, after a quick googling, I don’t think that the parsing mechanism for InitParams follows any coding scheme. Actually, it would be better if it were a fragment of the Query String URL request, which has fairly standard encoding and rules and is a command.

So, I think your only option is to use another delimeter, such as a pipe symbol.

eg:.

 key1=value1,key2=two|values 

If it was supposed to be a comma in the value for any reason, you can always do String.Replace ...

+4
source

I had the same issue and used URL encoding for my InitParams. I use the silverlightInitParams section in the web.config file to load several parameters at once and create the initParams line as follows:

 var initParams = new StringBuilder(); var initParamsFromConfig = (NameValueCollection)ConfigurationManager.GetSection("silverlightInitParams"); foreach (string key in initParamsFromConfig) { initParams.AppendFormat("{0}={1},", key, Server.UrlEncode(initParamsFromConfig[key])); } 

In the Silverlight client in Application_Startup, I retrieve the parameters and save them in the dictionary:

 foreach (var initParam in e.InitParams) { InitParameters.Add(initParam.Key, HttpUtility.UrlDecode(initParam.Value)); } 
+1
source

All Articles