在我的 ASP.NET MVC 应用程序中,我创建了一个 mailto
链接,其中主题和正文包含英镑 £ 符号。
这似乎在大多数情况下都有效,但一些使用 Outlook 2003/Chrome 的用户报告说,当单击链接时,英镑符号显示为 â£
,在我看来,它正在解释 UTF-8字符串为 ascii/windows-1252/whatever 等。
我不知道应该如何编码。目前我正在使用以下内容:
public static HtmlString EncodeMailTo(this HtmlHelper hlp, string val)
{
var encoded = HttpUtility.UrlEncode(val).Replace("+", "%20");
return new HtmlString(encoded);
}
并在视图中:
<a href="mailto:?subject=@(Html.EncodeMailTo(Model.Offer.Heading))&body=@(Html.EncodeMailTo(Model.Offer.Requirement))" >
Link
</a>
有没有一种方法可以在 UrlEncoding 之前将其转换为不同的编码(例如 Windows-1252)?我尝试转换字符串的编码,然后将其和使用的编码传递到 UrlEncode 但得到?而不是 £ 符号。
In my ASP.NET MVC application I am creating a mailto
link where the subject and body contain a pound £ symbol.
This appears to work in most cases but some users with Outlook 2003/Chrome is reporting that when clicking the link the pound symbol is showing as £
, which looks to me like it is interpreting a UTF-8 string as ascii/windows-1252/whatever etc.
I'm not sure how I should encode this. Currently I am using the following:
public static HtmlString EncodeMailTo(this HtmlHelper hlp, string val)
{
var encoded = HttpUtility.UrlEncode(val).Replace("+", "%20");
return new HtmlString(encoded);
}
and in the view:
<a href="mailto:?subject=@(Html.EncodeMailTo(Model.Offer.Heading))&body=@(Html.EncodeMailTo(Model.Offer.Requirement))" >
Link
</a>
Is there a way that I can convert this to a different encoding (e.g. Windows-1252) before UrlEncoding it? I've tried converting the encoding of the string and then passing this and the encoding used into UrlEncode but get ? instead of the £ symbols then.
发布评论
评论(3)
您不会找到任何一个答案可以解决所有浏览器/电子邮件客户端的这一问题。按照 的建议使用
£
在这种情况下,马特·费洛斯可能会为某些客户提供服务。但是,您会遇到没有命名 html 实体的其他字符或无法正确处理实体的浏览器/电子邮件客户端的问题。更烦人的是,如果您针对一种浏览器/电子邮件设置(例如使用 Windows-1252)更正它,则可能会破坏使用 UTF-8 的客户端。
最后,唯一真正的解决方案是浏览器和电子邮件客户端升级到使用 UTF-8,请参阅 避免国际邮件陷入漩涡。
You're not going to find any one answer that solves this problem for all browsers/email clients. Using the
£
as suggested by Matt Fellows may work on some clients in this instance. However, you'll run in to problems with other characters that do not have a named html entity, or browsers/email clients that do not handle entities correctly.Even more annoying is if you correct it for one browser/email set up (e.g. using Windows-1252) it might then break clients using UTF-8.
In the end, the only real solution is for browsers and email clients to upgrade to using UTF-8, see Avoiding an international mailto maelstrom.
尝试故意编码为
£
£ 符号不是基本 ASCII 字符集的一部分。所以 ASCII 不能正确解释它。
Try deliberately encoding as
£
The £ symbol is not part of the basic ASCII character set. So ASCII will not interpret it correctly.
增强 Matt Fellow 的答案+1
Enhancement on Matt Fellow's answer +1