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>");

How can I achieve this? I tried with Uri.EscapeDataString() method

Florin M.
  • 2,159
  • 4
  • 39
  • 97
  • 2
    Take a look here http://stackoverflow.com/questions/1517586/how-do-i-replace-all-the-spaces-with-20-in-c-sharp – Mark Mar 08 '16 at 07:41
  • Also, the code as you wrote it won't compile. You're missing a `+` between `href=" Uri.EscapeDataString`, and your testURL string literal should be surrounded by `"` instead of `'` – Audric de Schaetzen Mar 08 '16 at 07:44
  • Ohhh. My fault. That's happening when you see the accepted answer, but though you take into consideration the most voted comment. Should I delete this question? – Florin M. Mar 08 '16 at 07:45
  • No, don't delete it, flag it as a duplicate – Asons Mar 08 '16 at 07:49
  • Also, when I run the code you have, (with those 2 fixes), I get ` Just the URL String with href property www google.com/just test ` so there doesn't seem to be a mistake here? (ow, except that you're missing `"` in the href itself) – Audric de Schaetzen Mar 08 '16 at 07:49
  • could you try my answer, and see if it works that way? (seems to be more of a malformed HTML issue than a C# issue) – Audric de Schaetzen Mar 08 '16 at 07:53

4 Answers4

1

href should be in the form href="something"not href=something so replace

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

with

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

You could try

HttpUtility.UrlPathEncode(testURL)
0

You should try this

System.Web.HttpUtility.UrlEncode(string testURL)
Ahsan Qureshi
  • 59
  • 1
  • 8
0

Replace spaces with %20 in testURL.