C# 如何将字符串渲染为 html 链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11975483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how render string to html link
提问by Jeyhun Rahimov
I send some message to email as below:
我向电子邮件发送了一些消息,如下所示:
string link = "http://localhost:1900/ResetPassword/?username=" + user.UserName + "&reset=" + HashResetParams( user.UserName, user.ProviderUserKey.ToString() );
email.Body = link;
This string was sent to email, but shown as string, not as link, I want send it as link to click.
此字符串已发送到电子邮件,但显示为字符串,而不是链接,我想将其作为链接发送以单击。
采纳答案by codingbiz
Try this
尝试这个
string link = String.Format("<a href=\"http://localhost:1900/ResetPassword/?username={0}&reset={1}\">Click here</a>", user.UserName, HashResetParams( user.UserName, user.ProviderUserKey.ToString() ));
回答by Nick
Wrap linkin an anchor tag:
包裹link在一个锚标签中:
string link = '<a href="http://......">Click here to reset your password</a>';
and
和
email.IsBodyHtml = true;
Or combine them together using string concatenation and feed into email.Body. An email body is HTML, so it wont be a link unless you tell it to be one. Also, don't forget to tellit that the body is HTML, like I always do.
或者使用字符串连接将它们组合在一起并输入email.Body. 电子邮件正文是 HTML,因此除非您告诉它是一个链接,否则它不会是一个链接。另外,不要忘记告诉它正文是 HTML,就像我一直做的那样。
回答by Adriano Carneiro
Make it a link with the aHTML tag. And don't forget to set the MailMessageas HTML body:
使其成为带有aHTML 标记的链接。并且不要忘记设置MailMessage为 HTML 正文:
string link = "http://localhost:1900/ResetPassword/?username=" + user.UserName + "&reset=" + HashResetParams( user.UserName, user.ProviderUserKey.ToString() );
email.Body = "<a href='" + link + "'>" + link + "</a>";
email.IsBodyHtml = true;
回答by Phillip Schmidt
string link = "<a href=http://localhost:1900/ResetPassword/?username=" + user.UserName + "&reset=" + HashResetParams( user.UserName, user.ProviderUserKey.ToString() + "> Link Text Here </a>");
It doesn't know that it's a link :)
它不知道这是一个链接:)
回答by Andy Clark
Changes the email body from Plain text to Html and generate the link using an <a>element
将电子邮件正文从纯文本更改为 Html 并使用<a>元素生成链接
string link = @"<a href="www.mylink.com">link</a>"
email.IsBodyHtml = true;

