Guy
Guy

Reputation: 67380

Silverlight InitParams where value has a comma

In Silverlight 2, using C# on ASP.NET, you can pass a set of Initialization Parameters by assigning the Silverlight object's InitiParams with a string that's a series of comma separated key/value pairs.

I've seen other systems that have a similar mechanism for passing around collections of key/value pairs as a single string.

What is the solution for specifying a value that has a comma in it?

For example this string doesn't have a problem:

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

but this one does:

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

i.e. the "two,values" needs to have the comma escaped in some way...

Upvotes: 4

Views: 2595

Answers (2)

slfan
slfan

Reputation: 9129

I had the same problem and used URL encoding for my InitParams. I use a section silverlightInitParams in the web.config file to load several parameters at once and generate the initParams string like this:

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 within Application_Startup I extract the parameters and store them in a dictionary:

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

Upvotes: 1

Randolpho
Randolpho

Reputation: 56429

Unfortunately, after a quick Googling I don't think the parsing mechanism for InitParams follows any sort of encoding scheme. It'd actually be better if it were a URL Query String fragment, which has a fairly standard encoding and rules and is comma friendly.

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

E.g.:

key1=value1,key2=two|values

If that had to be a comma in the value for whatever reason, you could always do String.Replace...

Upvotes: 4

Related Questions