Pregunta

How to set image tag source from reading the key from AppSettings section in web.config in html file.

I am using one html file, in which there is an image tag and I want to set image source from web.config file key.

The web.config key is: <add key="Image" value="Image.gif" />

But i am not able to set the key on source.

I have tried this :

 <img src="<%= ConfigurationSettings.AppSettings["Image"] %>" width="685" height="177" />

but is not reading the source from there.

¿Fue útil?

Solución

Based on your comments, it looks like you're trying to generate a html file to be sent via email. Your better off creating a html file with place holders for the information you want to inject. Use a text reader to read the html from the file, and use regex or string.replace on the text replacing the place holders for the actual values.

You will need to have a full URL to images.

e.g.

To read the HTML

var HTML = EmailTemplate.GetHTMLFromTemplate(path to template)

 public static string GetHTMLFromTemplate(string templatepath)
    {
        var TemplateBody = new StringBuilder();

        using (var Reader = new StreamReader(templatepath))
        {
            string Line;
            while ((Line = Reader.ReadLine()) != null)
            {
                TemplateBody.Append(Line);
            }
        }

        return TemplateBody.ToString();
    }

Then replace the parts of the html

String.Replace("[image placeholder]", ConfigurationManager.AppSettings["Image"]);

The Email Template class looks like this (this is very old code!!)

public class EmailTemplate
    {
        public string Body { get; private set; }

        public static EmailTemplate GetTemplate(string body, string url, string html)
        {
            var AmendedHTML = new StringBuilder(html);
            AmendedHTML.Replace("[body]", body);
            AmendedHTML.Replace("../images", url + "images");

            var MyEmailTemplate = new EmailTemplate { Body = AmendedHTML.ToString() };

            return MyEmailTemplate;
        }

        public static EmailTemplate GetTemplate(string body, string title, string url, string year, string html)
        {
            var AmendedHTML = new StringBuilder(html);
            AmendedHTML.Replace("[BODY]", body);
            AmendedHTML.Replace("[HEADING]", title);
            AmendedHTML.Replace("../images", url + "images");
            AmendedHTML.Replace("[DATE]", year);
            AmendedHTML.Replace("[contactus.aspx]", url + "contactus/index.aspx");
            AmendedHTML.Replace("[unsubscribe.aspx]", url + "register/unsubscribe.aspx");

            var MyEmailTemplate = new EmailTemplate { Body = AmendedHTML.ToString() };

            return MyEmailTemplate;
        }

        public static string GetHTMLFromTemplate(string path)
        {
            var TemplateBody = new StringBuilder();

            using (var Reader = new StreamReader(path))
            {
                string Line;
                while ((Line = Reader.ReadLine()) != null)
                {
                    TemplateBody.Append(Line);
                }
            }

            return TemplateBody.ToString();
        }
    }

Otros consejos

try this: <img src="<%= System.Web.Configuration.WebConfigurationManager.AppSettings["Image"].ToString() %>" width="685" height="177" />

You want to replace a value in your .html email template.

You can do that by reading the file into memory, and replace a placeholder in your .html file. Change the image tag to something like <img src="$$image$$" width="685" height="177" />

Then in your code

string myString = "";
using (StreamReader reader = new StreamReader(Server.MapPath("~/Email/Product/<nameofthepage>.html"))) {
myString  = reader.ReadToEnd();   
}
myString = myString.Replace("$$image$$", ConfigurationSettings.AppSettings["Image"]);

then set the body property of your mail to myString

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top