Rob L
Rob L

Reputation: 2372

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

Upvotes: 1

Views: 864

Answers (2)

Fazeel Mehar
Fazeel Mehar

Reputation: 61

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

Upvotes: 0

Rob L
Rob L

Reputation: 2372

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)

Upvotes: 1

Related Questions