locked
using a <a> tag in text with dynamic href RRS feed

  • Question

  • User379720387 posted

    I sending emails to users which have a dynamic link in the body.

    It is ugly and does not look very professional.

    Any suggestions on how to do this?

    @{
        
        string link = "http://www.blahblah.com";
        
        string text = String.Format("click on this link here {0}", link);
    
        var url = Html.Raw("<a href=@link> here </a>");
    
        string textrev = "click on this link " + url;
    }
    
    <!DOCTYPE html>
    
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title></title>
        </head>
        <body>
    @text
            <br/>
    @textrev
        
        </body>
    </html>
    

    click on this link http://www.blahblah.com
    click on this link <a href=@link> link </a> 

    What I need is:

    click on this link here

    Thursday, January 1, 2015 11:26 AM

Answers

  • User-821857111 posted

    Html.Raw is used for rendering HTML. It shouldn't be used in your code block.

    @{
        string link = "http://www.blahblah.com";
        string text = String.Format("click on this link here {0}", link);
        var url = "<a href=\"" + link + "\"> here </a>";
        string textrev = "click on this link " + url;
    }
    
    <!DOCTYPE html>
    
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title></title>
        </head>
        <body>
    	@text
            <br/>
    	@Html.Raw(textrev)
        
        </body>
    </html>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 2, 2015 7:18 AM
  • User-821857111 posted

    VS stumbles on Html.Raw in the body of the WebMail helper.

    Html.Raw is not required when you pass HTML to the body of a mail message. It is only required when you need to display HTML as HTML in a Razor (.cshtml or .vbhtml) file.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 2, 2015 1:03 PM

All replies

  • User-735851359 posted

    Hello,

    try the following:

    @{
        string link = "click <a href=http://www.blahblah.com>here</a>";
        var res = Html.Raw(link);
    }
    
    @res

    Thursday, January 1, 2015 12:54 PM
  • User895691971 posted

    The question would be, why didn't you try to render it directly inside the HTML markup itself instead of creating variables and then writing those variables in the HTML and so on. I would've tried writing this logic as follows, 

    @{
       // Layout and other Page properties here
    }
    
    <!DOCTYPE html>
    
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title></title>
        </head>
        <body>
            <p>Click <a href="http://www.blahblah.com">here</></p>
        </body>
    </html>

    And if you're going to create any dynamic string to be sent, such as Tokens then you can simply just append that variable to the link. More like this, 

    @{
       var token = Guid.NewGuid().ToString();
    }
    
    <!DOCTYPE html>
    
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title></title>
        </head>
        <body>
            <p>Click <a href="http://www.blahblah.com/page?token=@token">here</></p>
        </body>
    </html>

    Now the token that is generated would be passed down and rendered into the HTML. To do that using the String.Format method, you might be writing something that looks like this code, 

    var token = Guid.NewGuid();
    
    String.Format("Your token is {0}", token);
    
    // or for a URL string
    
    String.Format("http://www.example.com/page?token={0}", token);

    The second method for generating the URL using this method can be tested at this fiddle: https://dotnetfiddle.net/gW3g3M 

    Good luck!

    Thursday, January 1, 2015 2:16 PM
  • User379720387 posted

    I am sending an email as per:

    WebMail.Send(
        to: emailto,
        subject: emailsubject,
        body: emailbody);

    @ayanmesut...

    the <a> tag has double quotes around the link. When I escape them with double quotes, Visual Studio does not like the results.

    @afzaal.ahmad....

    The url is very long, so I use a third party who shortens it. The end result is http://tiny.url/blahblah

    Prefer to have the entire link wrapped up in an <a> tag, so it is out of view.

    Everything is send by email, and I hope that I could send the string emailbody (see above) and that the email is rendered just showing here

    Thursday, January 1, 2015 3:05 PM
  • User-821857111 posted

    Html.Raw is used for rendering HTML. It shouldn't be used in your code block.

    @{
        string link = "http://www.blahblah.com";
        string text = String.Format("click on this link here {0}", link);
        var url = "<a href=\"" + link + "\"> here </a>";
        string textrev = "click on this link " + url;
    }
    
    <!DOCTYPE html>
    
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title></title>
        </head>
        <body>
    	@text
            <br/>
    	@Html.Raw(textrev)
        
        </body>
    </html>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 2, 2015 7:18 AM
  • User379720387 posted

    VS stumbles on Html.Raw in the body of the WebMail helper.

    But it works like this:

     WebMail.Send(
                    to: emailto,
                    subject: invoice,
                    body: textrev);

    Thanks.

    Friday, January 2, 2015 12:28 PM
  • User-821857111 posted

    VS stumbles on Html.Raw in the body of the WebMail helper.

    Html.Raw is not required when you pass HTML to the body of a mail message. It is only required when you need to display HTML as HTML in a Razor (.cshtml or .vbhtml) file.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 2, 2015 1:03 PM