Search code examples
asp.netmailto

how to dynamically generate root folder in link


I'm trying to dynamically generate root folder in a url that's in the body of a mailto hyperlink. Usually I can use page.resolveurl() to do this, but I'm not sure what to do in this case. Right now an email opens in outlook, however in the body of the email the "~" is not replaced by the root folder, but instead just displays "~/z/blog_listing.aspx?fid=489%26id={0}&id=1127."

Here's my code below that I'm having problems with.

code behind:

hypEmail.NavigateUrl = 
    Page.ResolveUrl(
        "mailto:[email protected]?Body=Check out this blog post I found: ~/z/blog_listing.aspx?fid=489%26id={0}", 
        cdata.PostID);

mark up page:

<asp:HyperLink ID="hypEmail" Text='Email' class="last" CommandName="Select" 
               runat="server"></asp:HyperLink>

Thanks!


Solution

  • Page.ResolveUrl is waiting for an url and won't magically format all urls in the string.

    You may replace your code with :

    string url = string.Format(
        "~/z/blog_listing.aspx?fid=489%26id={0}", 
        cdata.PostID
        );
    
    string body = "Check out this blog post I found: " + Page.ResolveUrl(url);
    hypEmail.NavigateUrl = string.Format(
        "mailto:{0}?Body={1}",
        "[email protected]",
        HttpUtility.UrlEncode(body)
        );