开发者

ASP.NET MVC using Url.Content with dynamic images

开发者 https://www.devze.com 2023-01-18 01:21 出处:网络
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.

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;
    }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号