从浏览器访问Web API Core文件夹中的图像

7
如何从Web API Core中的文件夹访问图像?我正在按以下方式操作。
https://localhost:44344/Uploaded_Documents/IMG_20190314_205925_3be1d2b3-3bac-4184-8468-995d58d5ff80.jpg

但是它会出现以下错误。

enter image description here

我的配置如下。

enter image description here

图片路径如下。 enter image description here 我该如何在Web API Core中通过浏览器访问这张图片?
1个回答

14

如果您想访问位于wwwroot文件夹之外的文件,您需要按照以下方式配置Static File Middleware:

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();

您可以参考 asp.net core 静态文件 Serve files outside of web root


API项目中的wwwroot目录? - Sujoy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接