有没有简单的方法将电子邮件地址字符串转换为正确的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.
提前致谢!
答案 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