0

My goal is to display an image on the page from the one of the folders, for example ~/Content/Images/img_1.jpg. The image paths are saved inside an array that I successfully fetch from a DB. What I am struggling with is the fact that there are special characters in the passed command.

So I'd have something like @Url.Content("~/Content/Images/img_1.jpg") to pass in the DB field and when it gets passed to JQuery .html() it doesn't work properly and gives a 404. I looked into escaping the characters but I don't think that works. This might be a bad way of doing the whole thing though, so I am open to ideas on how to facilitate that dynamic image loading...

EDIT: I have figured a bit out. The DB will hold only the image name, not the full path. So only the escaping the pathname characters in JS in HTML context are left.

"<img src=/Content/Images/" (escape characters here) + metadata.imgSrc + " alt=" + metadata.title + " >";

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
eddie evt
  • 59
  • 7
  • Please provide a Minimal, Reproducible Example: https://stackoverflow.com/help/minimal-reproducible-example – Twisty Sep 02 '21 at 16:19

1 Answers1

0

I escaped the string inside using the mapping from this answer.

So I got:

"<img src=&#x2F;Content&#x2F;Images&#x2F;" + metadata.imgSrc + " alt=" + metadata.title + " >";

With the image being placed in the respective directory.

eddie evt
  • 59
  • 7