2

How can I create a dynamic url to display an image?

I am creating a web page that lists search results of products and their associated images.

I tried using Url.Content, but it does not format the url correctly.

Code I created:

<img src="../../Images/<%: product.PicFileName %>" alt="photo" />

Html that was output:

<img src=".. ..="" Images="" nopic.jpg="" alt="photo" ="">

I also tried creating a helper method but it created the exact same output:

public static string GetPicUrl(string picFileName)
    {
        string picUrl = "../../" + picFileName;

        return picUrl;
    }

1 Answer 1

2

Just tried this:

<img src="<%: Url.Content("/Images/" + filename) %>" alt="foo" />

where filename = "foo.jpg" and it gives me <img src="/Images/foo.jpg" alt="foo"/> which links to http://hostname/Images/foo.jpg

Please let us know what format you want the Url in if this isn't what you had in mind and I will try to help you some more.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.