Question

I used to work in an mvc project and I had used the following code to load an image:

cshtml

 <img src="@Url.Action("GetImage", new { imageUrl =
 @asset.ImageUrl })" />

C#

public void GetImage(string imageUrl )
        {

            try
            {
                using (WebClientExtension client1 = new WebClientExtension())
                {
                    byte[] imageBytes = client1.ExecuteRequest(imageUrl );
                    if (imageBytes.Count() == 0)
                        throw new Exception();
                    HttpContext.Response.ContentType = "image/jpeg";
                    HttpContext.Response.OutputStream.Write(imageBytes, 0, imageBytes.Length);
                }
            }
            catch (Exception)
            {
                String FilePath;
                FilePath = Server.MapPath("/Images/no_image.png");
                byte[] imageBytes = System.IO.File.ReadAllBytes(FilePath);
                HttpContext.Response.ContentType = "image/jpeg";
                HttpContext.Response.OutputStream.Write(imageBytes, 0, imageBytes.Length);
            }

        }

Everything worked fine, and now my problem is use this in an ascx page. Obviously,@Url.Action("GetRenditionImage", new { renditionUrl = @asset.RenditionUrl }) wont work in ascx. How can I call a web method to achieve this?

Was it helpful?

Solution

Don't use @Url.Action. Use simple:

<img src="GetImage?imageUrl=sampleUrl" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top