c# - how do I append a cdn url to image tags? -
i working asp.net mvc application. outputting html on view supplied via application. want able take html , append cdn domain image tags. not sure how suggestions.
an easy way use html agility pack [link] in controller.
for example:
using htmlagilitypack; ... private string appendcdntoimgsrc(string htmlstring) { htmldocument htmldoc = new htmldocument(); htmldoc.loadhtml(htmlstring); // or htmldoc.load(htmlfilename) if file foreach(htmlnode img in doc.documentelement.selectnodes("//img[@src]") { htmlattribute attribute = img["src"]; attribute.value = attribute.value + ".cdn"; } // return string output... memorystream memstream = new memorystream(); htmldoc.save(memstream) memstream.seek(0, system.io.seekorigin.begin); streamreader reader = new streamreader(memstream); return reader.readtoend(); }
Comments
Post a Comment