How to encode a URL in jQuery/JavaScript and decode in ASP.NET

asp.net, c#, javascript, jquery, urlencode

Solution

Use `encodeURIComponent(str)` in JavaScript for encoding and use HttpUtility.UrlDecode to decode a URL in ASP.NET.

In JavaScript:

var url = "mynewpage.aspx?id="+encodeURIComponent(idvalue);
$(location).attr('href', url);

And in ASP.NET

_string _id = HttpUtility.UrlDecode(Request.QueryString["id"]);

Problem

How do you safely encode a URL using JavaScript such that it can be put into a GET string? Here is what I am doing in jQuery: ``` var url = "mynewpage.aspx?id=1234"; $(location).attr('href', url); ``` And in the ASP.NET page_load, I am reading this: ``` _string _id = Request.QueryString["id"].ToString(); ``` How can I encode the id in jQuery/JavaScript and decode in ASP.NET (C#)?

Original source

Related problems