URL 编码 - 非法字符替换

发布于 2024-10-09 16:33:23 字数 552 浏览 1 评论 0原文

我正在我目前正在从事的一个项目中做一些 URL 重定向。我是网络开发新手,想知道删除任何非法路径字符(例如“-”)的最佳实践是什么? 。

我希望我不必手动用编码后的 URL 替换每个字符

我尝试过 UrlEncode 和 HTMLEncode,但 UrlEncode 不适合?并且 HTMLEncode 不适合 '

例如,如果我要使用以下内容:

Dim name As String = "Dave's gone, why?"
Dim url As String = String.Format("~/books/{0}/{1}/default.aspx", bookID, name)

Response.Redirect(url)

我已经尝试像这样包装 URL:

Dim encodedUrl As String = Server.UrlEncode(url)

并且

Dim encodedUrl As String = Server.HTMLEncode(url)

I am doing some URL redirections in a project that I am currently working on. I am new to web development and was wondering what the best practise was to remove any illegal path characters, such as '-? etc.

I'm hoping I don't have to resort to manually replacing each character with their encoded URLs.

I have tried UrlEncode and HTMLEncode, but UrlEncode doesn't cater for the ? and HTMLEncode doesn't cater for '

E.g. If I was to use the following:

Dim name As String = "Dave's gone, why?"
Dim url As String = String.Format("~/books/{0}/{1}/default.aspx", bookID, name)

Response.Redirect(url)

I've tried wrapping URL like this:

Dim encodedUrl As String = Server.UrlEncode(url)

And

Dim encodedUrl As String = Server.HTMLEncode(url)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只怪假的太真实 2024-10-16 16:33:23

URL 规范(RFC 1738,12 '94)提出了一个问题,因为它将 URL 中允许的字符的使用限制为 US-ASCII 字符集的有限子集:

“...仅限字母数字 [0-9a-zA-Z],
特殊字符“$-_.+!*'(),”
[不包括引号 - ed],以及
用于它们的保留字符
可以使用保留的目的
URL 中未编码。”

所以我认为您需要担心?,并且在我的系统上

URL Encode; 转换: %7e%2fbooks%2f1%2fDave's+gone%2c+why%3f%2fdefault.aspx

现在,您在使用吗有任何网址重写吗?

The specification for URLs (RFC 1738, Dec. '94) poses a problem, in that it limits the use of allowed characters in URLs to only a limited subset of the US-ASCII character set:

"...Only alphanumerics [0-9a-zA-Z],
the special characters "$-_.+!*'(),"
[not including the quotes - ed], and
reserved characters used for their
reserved purposes may be used
unencoded within a URL."

So I think you need to worry about ?, and on my system

URL Encode; converts: %7e%2fbooks%2f1%2fDave's+gone%2c+why%3f%2fdefault.aspx

Now, are you using any url rewriting into this??

七秒鱼° 2024-10-16 16:33:23

尝试以下操作,

String.Format("~/books/{0}/{1}/default.aspx", bookID, Server.UrlEncode(name))  

String.Format("~/books/{0}/{1}/default.aspx", bookID, 
    Server.UrlEncodeComponent(name))

我认为它是 HttpUtility.UrlEncodeComponent ,请参阅 msdn 以获取更多信息。

Try following,

String.Format("~/books/{0}/{1}/default.aspx", bookID, Server.UrlEncode(name))  

String.Format("~/books/{0}/{1}/default.aspx", bookID, 
    Server.UrlEncodeComponent(name))

I think its HttpUtility.UrlEncodeComponent , please see msdn for further information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文