最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to encode a URL in jQueryJavaScript and decode in ASP.NET - Stack Overflow

programmeradmin4浏览0评论

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#)?

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#)?

Share Improve this question edited May 25, 2015 at 14:08 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Apr 20, 2012 at 15:02 Nick KahnNick Kahn 20.1k97 gold badges285 silver badges416 bronze badges 1
  • 1 This was answered pretty thoroughly here: stackoverflow./questions/2500013/… – Grant H. Commented Apr 20, 2012 at 15:06
Add a ment  | 

2 Answers 2

Reset to default 10

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"]);

I've always found this site wonderfully useful for figuring out which encoding I should be using (probably encodeURI or encodeURIComponent). You should be able to find what you want to use there.

I'm not as familiar with the ASP.NET side of things. These guys probably already have a great answer for you.

发布评论

评论列表(0)

  1. 暂无评论