URL编码报价和空间 [英] Url encoding quotes and spaces

查看:114
本文介绍了URL编码报价和空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个是带JavaScript的codeD某些查询的文字,但我已经遇到了一个使用案例,我可能要带code在服务器端相同的文字,编码这是发生不一样。我需要它是相同的。下面是一个例子。

I have some query text that is being encoded with JavaScript, but I've encountered a use case where I might have to encode the same text on the server side, and the encoding that's happening is not the same. I need it to be the same. Here's an example.

我输入我最喜欢的食物在搜索框中,并击中了搜索按钮。 JavaScript的EN codeS这是%22I%20like%20food%22

I enter "I like food" into the search box and hit the search button. JavaScript encodes this as %22I%20like%20food%22

比方说,我得到了相同的值,在服务器端的请求对象的字符串。它看起来像这样:\\我喜欢的食物\\

Let's say I get the same value as a string on a request object on the server side. It will look like this: "\"I like food\""

当我使用 HttpUtility.UrlEn code(值),结果是%22I + +一样的食物22%。如果我使用 HttpUtility.UrlPathEn code(值),结果是\\I%20like%20food \\

When I use HttpUtility.UrlEncode(value), the result is "%22I+like+food%22". If I use HttpUtility.UrlPathEncode(value), the result is "\"I%20like%20food\""

所以 UrlEn code 编码是我的引号,但使用的是 + 字符空格。 UrlPathEn code 编码是我的空间,但不是我的编码转义引号。

So UrlEncode is encoding my quotes but is using the + character for spaces. UrlPathEncode is encoding my spaces but is not encoding my escaped quotes.

我真的需要它做两件事,否则搜索code对我彻底borks(我有在搜索code无控制)。

I really need it to do both, otherwise the Search code completely borks on me (and I have no control over the search code).

提示?

推荐答案

UrlPathEn code 不逃避,因为它们不需要路径组件进行转义。

UrlPathEncode doesn't escape " because they don't need to be escaped in path components.

<一个href=\"http://msdn.microsoft.com/en-us/library/system.uri.escapedatastring.aspx\"><$c$c>Uri.EscapeDataString应该做你想做的。

Uri.EscapeDataString should do what you want.

这篇关于URL编码报价和空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆