2

My Question: Is it possible to change the way Attribute.Add formats the addition of the attribute?

I have an ASP.net website that loads a widget in a div, and I'm trying to find a way to add a data-options attribute to the div with my codebehind. I need the attribute to be created with a single quote around the data-options value instead of double quotes, because the value I'm assigning is a JSON pair.

What I need the attribute to look like: data-options='{“post_message_origin”:”https://www.mysite.com/MyWidget.aspx”}'

What it looks like when using Attribute.Add("data-options"): My code:

string dataoptions = "{\"post_message_origin\":\""+ HttpContext.Current.Request.Url.AbsoluteUri + "\"}";
MYWIDGET.Attributes.Add("data-options", dataoptions);

The attribute result: data-options="{“post_message_origin”:”https://www.mysite.com/MyWidget.aspx”}"

The set of double quotes encompassing the data-options value is preventing the JSON pair from being read correctly, hence my question.

I'm doing my best to avoid using hard coding so that I can easily load the page from development servers to production servers without changing the code, which is why I'm using HttpContext.Current.Request.Url.AbsoluteUri in the code behind instead of writing the data-options value straight to the div in the ASP markup.

David
  • 15
  • 12

2 Answers2

1

I would suggest using single quotes with the JSON, in this case. Either is acceptable, as long as they are in open-close pairs. This sidesteps the issue.

EDIT: Unfortunately, Attribute.Add encodes the quotes...

This has been brought up before. It looks like the long term solution is implementing your own encoder...

Community
  • 1
  • 1
Christopher Stevenson
  • 2,843
  • 20
  • 25
  • True, but it's only a side-step. It only works in this case because the OP appears to be in control of the data – John Saunders Feb 14 '14 at 19:47
  • I tried that once and the service that is using it has to have the JSON pairs in double quotes. For giggles, and to make sure I didn't mess it up the first time, I tried again, and if I put the JSON pair in single quotes, it actually prevents my widget from loading. I should have mentioned that in my post. – David Feb 14 '14 at 19:48
  • @Christopher Stevenson, I reviewed that post, but it deals with the encoding of the apostrophe vs the way that .net actually adds the attribute to the html when it adds the attribute. I've had no issues with apostrophe encoding when I've tested the use of apostrophes in the widget. I can't say that a custom encoder wouldn't work, I just don't think the post you linked and my question are repeats. – David Feb 14 '14 at 20:06
  • My main experience is in ASP.NET MVC, which doesn't use widgets. Hmm. Could you load the JSON in a ` – Christopher Stevenson Feb 14 '14 at 23:04
  • @ChristopherStevenson - I like that idea. I'll work on that later this week. I think the Widget Provider was having an issue as well, because at around 5:30PM on Friday, the data-options attribute was recognized by the widget, and I was able to receive the result of the data-options. I still like the idea of adding the data-options updated with js if it means I get my preferred attribute formatting. – David Feb 18 '14 at 17:19
0

I would recommend including neither kind of quote in your data:

string dataoptions = "{\"post_message_origin\":\""+ 
    HttpContext.Current.Request.Url.AbsoluteUri + "\"}";
dataoptions = dataoptions.Replace("\"", """).Replace("'", "'");
MYWIDGET.Attributes.Add("data-options", dataoptions);
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • I liked this approach, but when I tested, it hit the wall of the widget not loading when the JSON pair is surrounded with apostrophes. So unfortunately, the widget didn't load. – David Feb 14 '14 at 19:58
  • @David: sorry David, you have a broken widget that does not accept valid JSON. Maybe you can clean it up on the client side before the widget loads. – John Saunders Feb 14 '14 at 19:59
  • are you sure about the validity of JSON with apostrophes? From Google's JSON style guide: [link](https://google-styleguide.googlecode.com/svn/trunk/jsoncstyleguide.xml?showone=Double_Quotes#Double_Quotes) "If a property requires quotes, double quotes must be used. All property names must be surrounded by double quotes. Property values of type string must be surrounded by double quotes. Other value types (like boolean or number) should not be surrounded by double quotes." – David Feb 14 '14 at 20:11