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 £
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 £
public static HtmlString EncodeMailTo(this HtmlHelper hlp, string val)
{
var encoded = HttpUtility.UrlEncode(val).Replace("+", "%20").Replace("%C2%A3", "£");
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));
精彩评论