清理非英文字母以便在 URL 中安全使用
在 .NET 中,从帖子标题创建安全 URL 的最佳方法是什么? 例如,我可能有一篇文章“使用非英语进行测试”,我想将其转换为 URL 安全形式,同时仍然保持标题为 URL 的可读性目的,例如“测试使用非英语-”字符”,我将使用它们作为 http://mysite/blog/5/test-with-non-english-characters 我可以迭代字符串字符,或者保留所有字母(使用起来不安全)在 URL 中),或擦除所有非英语字符并将其变成下划线等(破坏了可读性,因为帖子将/可能是非英语语言)。我确信有一种方法可以将 ş
转换为 s
并将 Ğ
转换为 G
等,就像SQL Server 中不区分重音的搜索查询。
What is the best way, in .NET, to create safe URLs from titles of posts?
For example, I may have a post "test wıth non enğlİşh çharaçters" and I want to turn this into a URL safe form while still maintaining the readability purpose of the title being URL, such as, "test-with-non-english-characters", and I will use them as http://mysite/blog/5/test-with-non-english-characters I can just iterate over the string characters and either leave ALL letters as they are (not safe to use in URLs), or wipe out all non-English characters and turn them into an underscore etc. (breaks the purpose of readability, as posts will/may be in non English languages). I'm sure that there's a method for converting ş
to s
and Ğ
to G
etc. just like in an accent insensitive search query in SQL server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅我对另一个问题的回答,以删除字符串中的重音符号。
请注意,这并不能完全解决问题。例如,使用此代码不会将亚洲表意文字或非拉丁字母音译为英语(拉丁)字母。
See my answer to another question to remove accents from a string.
Note that this doesn't completely solve the problem. For example, Asian ideograms or non latin letters will not be transliterated to english (latin) letters using this code.