开发者

Pound symbol issue in mailto: link

开发者 https://www.devze.com 2023-04-12 13:13 出处:网络
In my ASP.NET MVC application I am creating a mailto link where the subject and body contain a pound £ symbol.

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开发者_如何学JAVA 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.


You're not going to find any one answer that solves this problem for all browsers/email clients. Using the &pound; 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.


Try deliberately encoding as &pound;

public static HtmlString EncodeMailTo(this HtmlHelper hlp, string val)
{
  var encoded = HttpUtility.UrlEncode(val).Replace("+", "%20").Replace("%C2%A3", "&pound;");

  return new HtmlString(encoded);
}

The £ symbol is not part of the basic ASCII character set. So ASCII will not interpret it correctly.


Enhancement on Matt Fellow's answer +1

var encoded = HttpUtility.UrlEncode(HttpUtility.HtmlEncode(val));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号