ASP.Net URLEncode Ampersand for use in Query Strin

2020-01-24 09:32发布

I need to redirect to a url passing a parameter as a query string.

This can include an Ampersand in the value. such as

string value = "This & That";
Response.Redirect("http://www.example.com/?Value=" + Server.UrlEncode(value));

This however returns http://www.example.com/?Value=This+&+That

What should I be using to encode this string?

EDIT: Thanks Luke for pointing out the obvious, the code does indeed work correctly. I Apologise, my question was not a valid question after all!

The page I was going to had a lot of old legacy code which is apparently doing some kinda of encoding and decoding itself making it appear as if my urlencode was not working.

My solution unfortunately is to completely drop use of an & until the code in question can be re-written. Don't you just hate old code!

7条回答
霸刀☆藐视天下
2楼-- · 2020-01-24 10:30

In .net core ver 2.1 ( late 2018 ) I was able to use the following:

System.Web.HttpUtility.UrlEncode(string_to_encode)
查看更多
登录 后发表回答