0

I have been trying for couple of days to display random image, with no success. The code rotates the images in folder < /images > , but always displaying the source location, look here:

enter image description here

Below you will find the code I have been playing with. Appreciate your cooperation..:)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace asp34_CasinoMegaChallenge
{
public partial class asp34 : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        GetFiles();
    }

    public string GetFiles()
    {
        Random random = new Random();

        string[] randomImages=Directory.GetFiles(HttpContext.Current.Server.MapPath("~/Images/"));

        Image1.ImageUrl = randomImages[random.Next(randomImages.Length)];

        return Image1.ImageUrl;       
    }

    }   
}
Izzy
  • 6,740
  • 7
  • 40
  • 84
phi1618
  • 47
  • 1
  • 1
  • 3
  • 1
    When you examine the values in `randomImages`, I think you'll find that they're filesystem paths, not relative URL's. You need to provide a URL to the images that is valid on the client. – mason Jan 19 '15 at 15:09
  • yes, that is the point. How to do so..? – phi1618 Jan 19 '15 at 15:56
  • possible duplicate of [Absolute path back to web-relative path](http://stackoverflow.com/questions/3164/absolute-path-back-to-web-relative-path) – mason Jan 19 '15 at 15:59

0 Answers0