Search code examples
c#asp.net-mvcrazorasp.net-mvc-routingurl.action

When generating URL in the controller, the url is not clickable


I saw some examples here, but I'm actually doing the same thing, however, it does not fully work. So, this is my question:

I have the code in my controller to generate 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>";

Then I send an email:

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

This is my Send Email code:

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 email is received:

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>

The URL is built successfully, however, it is not clickable.

What am I missing?


Solution

  • When an HTML anchor is rendered, it requires at minimum an href and some sort of object to click on. That object can either be text or an image. The problem is that you have specified no text, so there is nothing for the user to click on.

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

    You need to add something between <a></a> to form a hyperlink.

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

    Another thing to check is whether or not your MailMessage.IsBodyHtml is set to true. If it is false, your email will be processed as plain text instead of HTML, which means it won't process hyperlinks.