如何在jQuery/JavaScript中编码URL并在ASP.NET中解码

Nic*_*ahn 4 javascript c# asp.net jquery urlencode

如何使用JavaScript安全地对URL进行编码,以便将其放入GET字符串?

这是我在jQuery中所做的:

var url = "mynewpage.aspx?id=1234";
$(location).attr('href', url);
Run Code Online (Sandbox Code Playgroud)

在ASP.NET page_load中,我正在读这个:

_string _id = Request.QueryString["id"].ToString();
Run Code Online (Sandbox Code Playgroud)

如何在jQuery/JavaScript中编码id并在ASP.NET(C#)中解码?

Gov*_*iya 9

使用encodeURIComponent(str)JavaScript中的编码和使用HttpUtility.UrlDecode解码在ASP.NET的URL.

在JavaScript中:

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

在ASP.NET中

_string _id = HttpUtility.UrlDecode(Request.QueryString["id"]);
Run Code Online (Sandbox Code Playgroud)


vee*_*ain 2

我一直发现这个网站对于确定我应该使用哪种编码(可能encodeURI是 或encodeURIComponent)非常有用。您应该能够在那里找到您想要使用的内容。

我对 ASP.NET 方面不太熟悉。这些人可能已经为您提供了很好的答案。