在控制器中生成URL时,该URL不可单击

时间:2018-02-01 22:33:57

标签: c# asp.net-mvc razor asp.net-mvc-routing url.action

我在这里看到了一些例子,但我实际上做了同样的事情,但是,它并没有完全发挥作用。所以,这是我的问题:

我在控制器中有代码来生成URL:

string link = @Url.Action("Index", "Details", new System.Web.Routing.RouteValueDictionary(new { id = newId }), "http", Request.Url.Host);
var strUrl = "<a href='"
    + @Url.Action("Index", "Details", new System.Web.Routing.RouteValueDictionary(new { id = Id }), "http", Request.Url.Host)
    + "'>Click here</a>";

然后我发送电子邮件:

var response = EmailHelper.SendApproveTicketEmail(myModel.Id, strUrl);

这是我的发送电子邮件代码:

public static SaveResult SendEmail(int ID, string Url)
{
    var email = new Email
    {
        Recipients = Configuration.EmailTo,
        Body = Configuration.EmailBody + ID.ToString() + "." + System.Environment.NewLine + "Click the following link to open the details page:" + ticketUrl,
        Sender = Configuration.EmailFrom,
        Subject = Configuration.EmailSubject,
        Cc = Configuration.EmailCC
    };

    return FormatResult(_client.SendEmail(Configuration.ApplicationName, 0, email));
}

收到以下电子邮件:

The following item needs to be approved:0011 Click the following link to open the item details page:<a href='http://localhost:51335/Details/Index/001'>Go to your item</a>

网址已成功构建,但无法点击。

我错过了什么?

1 个答案:

答案 0 :(得分:2)

呈现HTML锚点时,至少需要href和某种对象才能点击。该对象可以是文本或图像。问题是您没有指定任何文本,因此用户无需单击。

var ticketUrl = "<a href='"
    + @Url.Action("Index", "TicketDetail", new System.Web.Routing.RouteValueDictionary(new { id = newTicketId }), "http", Request.Url.Host)
    + "'></a>";

您需要在<a></a>之间添加内容以形成超链接。

var ticketUrl = "<a href='"
    + @Url.Action("Index", "TicketDetail", new System.Web.Routing.RouteValueDictionary(new { id = newTicketId }), "http", Request.Url.Host)
    + "'>Click Me</a>";

要检查的另一件事是您的MailMessage.IsBodyHtml是否设置为true。如果是false,您的电子邮件将以纯文本而非HTML格式处理,这意味着它不会处理超链接。