2016-03-08 110 views
0
string testURL = 'www.google.com/just test' 

StringBuilder emailBody = new StringBuilder(); 

emailBody.Append("<tr>"); 
emailBody.Append("<td> Just the URL </td>"); 
emailBody.Append("<td> String with href property </td>"); 
emailBody.Append("</tr>"); 
emailBody.Append("<tr>"); 
emailBody.Append("<td>" + testURL + "</td>"); // it displays: www.google.com/just test 
emailBody.Append("<td> <a href=" + Uri.EscapeDataString(testURL) + ">" + name + "</a> </td>"); 
// the href property displays only: www.google.com/just 
emailBody.Append("</tr>"); 

我该如何做到这一点?我Uri.EscapeDataString()方法用href属性中的空格链接

+2

这里http://stackoverflow.com/questions/1517586/how-do-看看i-replace-all-spaces-with-20-in-c-sharp – Mark

+0

此外,您编写的代码不会编译。你在'href =“Uri.EscapeDataString'之间缺少一个'+',你的testURL字符串应该被'''包围,而不是''' –

+0

Ohhh。我的错。当你看到接受的答案时,就会发生这种情况,但是你考虑的是最多的评论。我应该删除这个问题吗? –

回答

0

试过你可以尝试

HttpUtility.UrlPathEncode(testURL) 
+0

然后它返回“”,我试过了 –

0

你应该试试这个

System.Web.HttpUtility.UrlEncode(string testURL) 
0

在testURL替换20%的空间。

1

HREF应在形式href="something"href=something 所以更换

emailBody.Append("<td> <a href=" + Uri.EscapeDataString(testURL) + ">" + name + "</a> </td>"); 

emailBody.Append("<td> <a href=\"" + Uri.EscapeDataString(testURL) + "\">" + name + " </a> </td>");