将字符串转换为正确的URI格式?

时间:2012-01-03 08:51:17

标签: c# asp.net html

有没有简单的方法将电子邮件地址字符串转换为正确的URI格式?

输入:

http://mywebsite.com/validate_email/3DE4ED727750215D957F8A1E4B117C38E7250C33/myemail@yahoo.com  

输出应为:

http://mywebsite.com/validate_email/3DE4ED727750215D957F8A1E4B117C38E7250C33/myemail%40yahoo.com  

如果我没有输出,我会收到类似

的错误回复
An Error Was Encountered
The URI you submitted has disallowed characters.

提前致谢!

1 个答案:

答案 0 :(得分:10)

描述

您必须从网址中提取QueryString,对其进行编码并构建新网址。

示例

string url = "http://mywebsite.com/validate_email/3DE4ED727750215D957F8A1E4B117C38E7250C33/myemail@yahoo.com";
int index = url.LastIndexOf("/");

string queryString = url.Substring(index + 1, url.Length - (index + 1));
url = url.Substring(0, index) + "/" + HttpUtility.UrlEncode(queryString);

// url is now
// http://mywebsite.com/validate_email/3DE4ED727750215D957F8A1E4B117C38E7250C33/myemail%40yahoo.com

更多信息