Nuno Gomes /* ASP.NET Adventures */

var myInterests = new { language : "C#", technology: "ASP.NET" };

ASP.NET – Converting C# String to JSON String

Most web applications display messages to users. Displaying messages is the most effective way to inform user about errors and warnings or to simply display info or success status.

I also believe that most of those web applications renders user messages using HTML elements such as Div, Span or Label.

The others, mainly because their complex layout or business rules, have to render those messages using a different approach: they render each display message as a Javascript function call where the message itself is a function parameter.  Such call could be simply like this:

string.Format("top.addMessage(\"{0}\");", message)

Those of you that already faced this problem won’t learn nothing new but I hope to alert the others and avoid them to face a problem that usually is only detected in advanced develop stage or even production environment.

Problem

The problem turns visible when the display message contains some special characters that prevent the function call to execute and trigger javascript errors.

Typically the characters are “ and ‘ but there are more.

Well, that is not completely correct, only the character “ is problematic, since he is the JSON string delimiter character, but, javascript engines also accept the character ‘ as a delimiter. The best way to avoid problems with ‘ is to ensure that the correct delimiter is used.

By now, you should be thinking that your applications are immune to this problem. Tell me, do you use string Resources? Most of us use, it’s a best practice and a smart choice. Also tell me, do you manage production string Resources? Most of us don’t. In fact, the application owner or sponsor can change it completely outside of your control scope.

String Resources are one of the major vectors of this problem but you can get this problem simply by trying to display an exception message:

string.Format("top.addMessage(\"{0}\");", exception.ToString())

Solution

What we are needing is an JScript.Encode method or similar that encode a C# string in such a way that it becomes a JSON encoded string.

Such method is not available on NetFX but, fortunately, Rick Strahl made a great post about this problem and made available a method that fits perfectly our needs.

Now that you know about it … use it.

Posted: May 23 2009, 12:09 AM by nmgomes | with 48 comment(s)
Filed under:

Comments

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# May 24, 2009 6:31 PM

Bruno Campagnolo de Paula weblog » Resumo do dia para 2009-05-25 said:

Pingback from  Bruno Campagnolo de Paula weblog » Resumo do dia para 2009-05-25

# May 25, 2009 11:49 PM

Cafliage said:

Very informative post. Thanks for taking the time to share your view with us.

# November 28, 2010 2:55 PM

eluchelereeva said:

You certainly have some agreeable opinions and views. Your blog provides a fresh look at the subject.

# January 24, 2011 4:45 PM

apples weight loss said:

I find myself coming to your blog more and more often to the point where my visits are almost daily now!

# February 12, 2011 3:06 PM

las vegas car auctions said:

Awesome Post. I add this Blog to my bookmarks.

# February 20, 2011 2:30 AM

anirwayjady said:

Awesome Blog. I add this Blog to my bookmarks.

# February 25, 2011 2:37 AM

Uriliopsili said:

Great Blog. I add this Blog to my bookmarks.

# February 26, 2011 7:05 PM

buyers guide car said:

I’ve been visiting your blog for a while now and I always find a gem in your new posts.  Thanks for sharing.

# March 1, 2011 8:03 PM

bahglyday said:

I find myself coming to your blog more and more often to the point where my visits are almost daily now!

# March 3, 2011 3:20 PM

fouradarams said:

Thank You For This Blog, was added to my bookmarks.

# March 6, 2011 3:31 AM

KemeImmolfefe said:

I just sent this post to a bunch of my friends as I agree with most of what you’re saying here and the way you’ve presented it is awesome.

# March 18, 2011 8:34 PM

volvo v40 diesel used cars said:

I find myself coming to your blog more and more often to the point where my visits are almost daily now!

# April 5, 2011 9:56 AM

alorioulajala said:

hi, visit my soft blog http://rulogamp.blog.com/

# April 17, 2011 6:39 PM

cheap new car said:

I just sent this post to a bunch of my friends as I agree with most of what you’re saying here and the way you’ve presented it is awesome.

# April 19, 2011 7:02 PM

Vogslotly said:

Great Post. I add this Post to my bookmarks.

# April 27, 2011 4:48 PM

new york auto auctions said:

I just book marked your blog on Digg and StumbleUpon.I enjoy reading your commentaries.

# April 30, 2011 5:41 AM

new jersey auto auction said:

I just book marked your blog on Digg and StumbleUpon.I enjoy reading your commentaries.

# May 1, 2011 8:36 PM

enganoWah said:

ipage web hosting  information

# June 17, 2011 7:28 AM

licaHypespeep said:

cheap vps server - UK

# June 24, 2011 11:43 AM

justhosthosting said:

justhost hosting top webhosting  technologycanada.com/justhost-hosting

# July 15, 2011 7:20 AM

twingigma said:

Very informative post. Thanks for taking the time to share your view with us.

# July 16, 2011 4:51 PM

ipageweb said:

ipage review , cheap host  technologycanada.com/ipage-hosting

# July 17, 2011 2:04 AM

Allerblauntee said:

I find myself coming to your blog more and more often to the point where my visits are almost daily now!

# July 17, 2011 3:07 PM

hostgator hosting said:

hostgator review , best webhosting  technologycanada.com/hostgator-hosting

# July 18, 2011 9:58 AM

Cheap oem software said:

hu8inw Every time I come back here again and don`t get disappointed..!

# September 24, 2011 7:39 AM

Jake Molder said:

I just book marked your weblog on Digg and Stumble Upon.I delight in reading your commentaries.

# November 2, 2011 10:29 PM

Pozycjonowanie Top said:

Hi there! Do you know if they make any plugins to safeguard against hackers? I'm kinda paranoid about losing everything I've worked hard on. Any tips?

# December 14, 2011 4:09 AM

Moogelisabeth said:

order an  for more detail   , just clicks away

# December 17, 2011 6:00 AM

biamshumberto said:

check <a href=name-brandbags.livejournal.com/>fake name-brand purses</a>  for more detail   for less

# December 30, 2011 11:35 PM

Moogetonisha said:

look at <a href=www.despursesok.com/>designer purses</a>   to take huge discount   with low price

# January 2, 2012 7:42 PM

Lessyghislaine said:

check this link, <a href=www.designer-resale.org/>affordable handbags</a>   to take huge discount   , for special offer

# January 3, 2012 4:11 AM

icorslory said:

I'm sure the best for you <a href=www.dvdripper.org/>dvd ripper</a>  online   for gift

# January 3, 2012 7:01 PM

Moogelennie said:

click to view <a href=2011chanel.livejournal.com/>2011 chanel</a>  for more   with confident

# January 4, 2012 1:03 PM

Lessyjanyce said:

click to view <a href=www.e-louisvuittonbags.com/chanel-handbags-c-5.html>chanel handbags</a>   for promotion code    and check coupon code available

# January 4, 2012 5:00 PM

spider repellent said:

I am curious to find out what blog platform you're using? I'm experiencing some small security problems with my latest blog and I would like to find something more secure. Do you have any recommendations?

# January 4, 2012 10:01 PM

beedoivana said:

you definitely love <a href=dvd-to-mov.weebly.com/>dvd to mov</a>  to your friends   to your friends

# January 5, 2012 6:04 AM

Moogeeusebio said:

I'm sure the best for you <a href=name-brandbags.livejournal.com/>name-brand purse</a>   and check coupon code available    for promotion code

# January 5, 2012 3:37 PM

biamsrosamond said:

view <a href=chanelspring.eklablog.com/>chanel spring</a>  to your friends   suprisely

# January 8, 2012 4:04 PM

icorsdaysi said:

look at <a href=new-chanel-handbags.weebly.com/>new chanel handbags</a>  for less   suprisely

# January 9, 2012 5:58 PM

biamscornelia said:

view <a href=style-chanel.weebly.com/>style chanel</a>  for less    and get big save

# January 10, 2012 4:02 AM

puntyquinn said:

check this link, <a href=www.7star-mirror-handbags.com/>mirror bag</a>  suprisely   for less

# January 10, 2012 11:55 PM

nakedcelebritiesh said:

kam investirati denar <a href=http://www.publikumsi.info>publikum.si</a>

# January 20, 2012 7:42 PM

Moogeharriette said:

buy best <a href=dvd-to-m4v.weebly.com/>dvd to m4v</a>  with low price   for less

# January 26, 2012 1:30 AM

Quomsazzie said:

check <a href=dvd-to-avi.weebly.com/>dvd to avi</a>   for promotion code   to your friends

# January 28, 2012 11:07 PM

UseskSon said:

buy <a href=boutique-vuitton.weebly.com/>boutique vuitton</a>  online shopping

# January 29, 2012 1:57 PM

Lessyladonna said:

get <a href=dvd-to-wmv-converter.weebly.com/>dvd to wmv converter</a>  with low price   with confident

# February 7, 2012 9:38 AM

nakedj said:

# February 8, 2012 9:08 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)