Set Image Source from Handler an subsequent page calls

Viewed 22

I am setting the source for an image using a handler following this example: Use Razor Page handler as image source.

The code looks like this:

<img src="StreamLink?handler=Thumb&filename=@thumbpath"/>

This works great for the first time the page is called. However, there is an option to call the same page (Albumlist) again for a different year using this link:

<a class="yearlist" asp-page="/Albumlist" asp-route Section="@strYear.Key">@strYear.Value</a>

On the second call OnGetThumb is no longer executed.

Any ideas on how to set the image source for subsequent page calls?

Thanks

Th.

1 Answers

You need to make sure the url in src is correct.Here is a working demo:

Index.cshtml:

<a class="yearlist" asp-page="/Test2/Albumlist" Section="1">test</a>

Test2/Albumlist.cshtml:

<img src="Albumlist?handler=Thumb&filename=1" />

Test2/Albumlist.cshtml.cs:

public class AlbumlistModel : PageModel
    {
        public void OnGet()
        {
        }
        public FileResult OnGetThumb(string fileName)
        {
            string path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/imgs/"+ fileName + ".jpg");

            //Read the File data into Byte Array.
            byte[] bytes = System.IO.File.ReadAllBytes(path);

            return File(bytes, "image/png");
        }
    }

result: enter image description here

Related