'ASP NET Core Web API returning a link to an image failing

I have a working ASP.NET Web API. I am currently converting it to ASP.NET Core 3.1.

In one of the API calls, I check if an image is present in a folder. If it is not, I create it (in code) and then send a link to it (amongst other things) back to the caller. The check if exists and creation of the image works fine in ASP.NET Core, but when I send back the link to the image, the client does not find it and hence it appears as a broken link on their web page.

See this diagram for the structure and for example the EnabledTRImage.png file.

enter image description here

The Url I am returning is:

http://localhost:59682/TR/128/EnabledTRImage.png

I have also tried returning:

http://localhost:59682/wwwroot/TR/128/EnabledTRImage.png

But this fails too.

One thing I have noticed is that in ASP.NET Core, creating the image in code makes it part of the Project, whereas in ASP.NET it does not. I was wondering if that had anything to do with the issue.

Unfortunately, these images are central to the Web API. So I am stuck.

Any ideas? Thanks



Solution 1:[1]

I just fixed it myself. I had not specified:

app.UseStaticFiles();

In the Startup/Configure method. That line is not there in the WebApi template (as expected I guess)

Solution 2:[2]

In the Startup/Configure method.

Instead of app.UseStaticFiles();

Specify your folder path in StaticFileOptions()

app.UseStaticFiles(new StaticFileOptions(){
        FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"ProductImagesFiles")),
    RequestPath = new PathString("/ProductImagesFiles")
    });

'enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Fazeel Mehar