简体   繁体   English

为什么在某些情况下Request.QueryString用空字符替换+?

[英]why Request.QueryString replace + with empty char in some cases?

I have a problem that if I pass a string that contain + in a query string and try to read it , it get the same string but by replacing + with empty char 我有一个问题,如果我在查询字符串中传递包含+的字符串并尝试读取它,它将得到相同的字符串,但是用空字符替换+
For example if i pass query like ../Page.aspx?data=sdf1+sdf then in page load I read data by data = Request.QueryString["data"] it will get as below data ="sdf1 sdf" 例如,如果我通过../Page.aspx?data=sdf1+sdf查询,则在页面加载中,我通过data = Request.QueryString["data"]读取data ,它将得到如下data ="sdf1 sdf"
I solve the problem by replacing any empty char with + .. 我通过用+ ..替换任何空字符来解决问题。

But Is there any problem that cause that ? 但是,是否有引起该问题的问题? and Is my solution by replacing empty char with + is the best solution in all cases ? 并且在所有情况下,用+代替空字符的解决方案是否是最佳解决方案?

Because + is the url encoded representation of space " " . 因为+是URL编码的空间" "表示。 If you want to preseve the plus sign in your value you will need to url encode it: 如果您想在值中使用加号,则需要对其进行url编码:

"/Page.aspx?data=" + HttpUtility.UrlEncode("sdf1+sdf")

which will produce: 会产生:

/Page.aspx?data=sdf1%2bsdf

Now when you read Request.QueryString["data"] you will get what you expect. 现在,当您阅读Request.QueryString["data"]您将获得期望的结果。

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

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