简体   繁体   English

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

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

How do you safely encode a URL using JavaScript such that it can be put into a GET string? 如何使用JavaScript安全地对URL进行编码,以便将其放入GET字符串?

Here is what I am doing in jQuery: 这是我在jQuery中所做的:

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

And in the ASP.NET page_load, I am reading this: 在ASP.NET page_load中,我正在读这个:

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

How can I encode the id in jQuery/JavaScript and decode in ASP.NET (C#)? 如何在jQuery / JavaScript中编码id并在ASP.NET(C#)中解码?

Use encodeURIComponent(str) in JavaScript for encoding and use HttpUtility.UrlDecode to decode a URL in ASP.NET. 在JavaScript中使用encodeURIComponent(str)进行编码,并使用HttpUtility.UrlDecode解码ASP.NET中的URL。

In JavaScript: 在JavaScript中:

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

And in ASP.NET 在ASP.NET中

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

I've always found this site wonderfully useful for figuring out which encoding I should be using (probably encodeURI or encodeURIComponent ). 我总是发现这个网站非常有用,可以确定我应该使用哪种编码(可能是encodeURIencodeURIComponent )。 You should be able to find what you want to use there. 你应该能够找到你想在那里使用的东西。

I'm not as familiar with the ASP.NET side of things. 我对ASP.NET的方面并不熟悉。 These guys probably already have a great answer for you. 这些家伙可能已经为你找到了一个很好的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM