在MVC剃刀mailto连结编码

在MVC剃刀mailto连结编码

本文介绍了在MVC剃刀mailto连结编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的模型

public class EmailLinkModel
{
    public string mailbody { get; set; }
    public string emailSubject { get; set; }
    public string emailto { get; set; }
}

mailbody包含了实际的文本是邮件主体的一部分。本文包含一个长的文本[包含特殊字符,例如? <等,可以包含换行空格等]
我想创建的局部视图一个mailto HTML标签和我试图3种不同的方法,在某些时候我的mailto链接无法打开,默认邮件客户端
我想原因是Html或网址ecnoding [我显示在文本区域的mailbody内容,以及在这里,以确保值正确即将]
这是我的看法。

mailbody contains the actual text which is a part of mail body. This text contains a long text [ contains special characters like ? < etc and can contain line breaks spaces etc]I want to create a mailto html tag in the partial view and i tried 3 different approaches and at some point my mailto link not opening the default mail clientI think the reason was Html or Url ecnoding [ I am showing the mailbody content in a text area as well here to make sure value is coming correctly]Here is my view

@model RoyaltyDb.Models.EmailLinkModel
@{
    Layout = null;
}
<div class="row ">
    @{
        var formatted_doc_data = Model.mailbody;
        /*
        var formatted_doc_data =HttpUtility.HtmlEncode(Model.mailbody);
          */

      /*  var formatted_doc_data = Model.mailbody.ToString().Trim().Replace("\n", "%0D%0A");
        formatted_doc_data = formatted_doc_data.ToString().Trim().Replace(" ", "%20");
        formatted_doc_data = formatted_doc_data.ToString().Trim().Replace(" ", "%20");
        formatted_doc_data = formatted_doc_data.ToString().Trim().Replace("%", "%25");
        formatted_doc_data = formatted_doc_data.ToString().Trim().Replace("?", "%3F");
        formatted_doc_data = formatted_doc_data.ToString().Trim().Replace("/", "%2F");
        formatted_doc_data = formatted_doc_data.ToString().Trim().Replace(":", "%3A");*/
    }
    <a class="btn btn-primary center" href="mailto:@[email protected]&body=@formatted_doc_data" target="_top">Send Mail</a>
</div>
<hr/>
<div class="row">
    @Html.TextAreaFor(m => m.mailbody, new { style = "width: 100%; height:200px;margin:5px 5px;" })
</div>

我怎样才能做到正确的编码创建的mailto链接

How can i do proper encoding to create the mailto Link

推荐答案

尝试 Url.En code 方法,你可能在已经错过了一些特殊字符的code手动编码。

Try Url.Encode method, as you might have missed some special characters in your code for manual encoding.

<a class="btn btn-primary center"
   href="mailto:@[email protected]&[email protected](Model.body)">Send Mail</a>

另外请注意,有可能是在的href 属性的长度是有限的。请参见了解更多详情。

Also please note, that there might be a limit for the length of the href attribute. See this answer for more details.

这篇关于在MVC剃刀mailto连结编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 23:03