2016-03-08 114 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>");