GOPAL SHARMA
GOPAL SHARMA

Reputation: 687

Access image from Web API Core Folder from Browser

How can access image from folder from web api core? I am doing as below

https://localhost:44344/Uploaded_Documents/IMG_20190314_205925_3be1d2b3-3bac-4184-8468-995d58d5ff80.jpg

But it is giving below error.

enter image description here

Me configuration is like below.

enter image description here

Image path is like below. enter image description here

How can I access this image from browser in web api core ?

Upvotes: 7

Views: 7239

Answers (1)

Ryan
Ryan

Reputation: 20116

If you would like to access your file out of wwwroot folder,you need to configure the Static File Middleware as follows:

app.UseStaticFiles();// For the wwwroot folder

app.UseStaticFiles(new StaticFileOptions
        {
            FileProvider = new PhysicalFileProvider(
                Path.Combine(Directory.GetCurrentDirectory(), "Uplodaed_Documents")),
            RequestPath = "/Uplodaed_Documents"
        });
//Enable directory browsing
app.UseDirectoryBrowser(new DirectoryBrowserOptions
        {
            FileProvider = new PhysicalFileProvider(
                Path.Combine(Directory.GetCurrentDirectory(), "Uplodaed_Documents")),
            RequestPath = "/Uplodaed_Documents"
        });

app.UseMvc();

You could refer to asp.net core static files Serve files outside of web root

Upvotes: 14

Related Questions