nkacolz
nkacolz

Reputation: 224

Passing params through URL - Single quotes become '

I'm working with a ASP.NET project. We are passing params through the url and every time a single quote is passed the url changes all single quotes to %27 and the actual value read in through the javascript changes all single quotes to '

Can someone tell me how I can maintain the single quotes as our parameters need to match the exact values. This is my code.

public class Model
{
   public string example {get; set;}
}

public ActionResult Index(string example)
{
   var model = new Model();
   model.example = example;
   return View(model);
}

Index.cshtml at the bottom ---------------------

<script type="text/javascript">
   var example = "@Model.example";
   Main();
</script>

Javascript -----------------

console.log(example);

Examples: www.example.com?example=Turtle's_Are_Cool Changes the url instantly to => www.example.com?example=Turtle\%27s_Are_Cool and the JavaScript outputs Turtle&#39s_Are_Cool

Upvotes: 1

Views: 1493

Answers (1)

Bharat
Bharat

Reputation: 1205

If you want to do it on the server side you can use Server.URLEncode("Turtle's_Are_Cool"))

If you want to manage the same on client-side you can replace ' with '

example = example.replace(/'/g, "\'");

But if you have got a single quote coming from server-side and you want to convert it at client-side then the easiest way is to use HTML element as shown in the below question

Unescape apostrophe (&#39;) in JavaScript?

Upvotes: 1

Related Questions