无法读取asp.net网站核心
问题描述:
我有一个asp.net核心API的项目,我想读的是存在于我的网站的图像,但我收到404找不到错误的图像。无法读取asp.net网站核心
低于代码行抛出异常时执行GetStreamAsync方法:
foreach (var item in imgUrls)
{
// read from remote image drive
using (HttpClient c = new HttpClient())
{
using (Stream s = await c.GetStreamAsync(item))
{
// do something with the stream...
}
}
}
图片网址是正确的,但是当我尝试应用程序运行时将其粘贴在另一个选项卡中,我没有看到图片显示在我的浏览器中。我错过了一个asp.net配置,或者是通过api响应禁止显示内容文件(如图像)的东西。
注:我们送imageUrls是FQDN的URL,例如URL看起来像:
http://localhost:25071/images/test.jpg
期待您的帮助。
答
这是我固定的问题,我没有让我的asp.net核心web API目录浏览。
,所以我说我的静态文件(影像)文件夹下的wwwroot和startup.cs下面的代码添加到配置/允许目录浏览到图像文件夹和应用程序开始交付,并显示图像。
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// few standard lines in here.....
// enable directory browsing
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\images")),
RequestPath = new PathString("/images")
});
app.UseDirectoryBrowser(new DirectoryBrowserOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\images")),
RequestPath = new PathString("/images")
});
}
享受!
你可以粘贴API代码也? –
你不发表您的中间件配置,如果没有它,我们注定要猜 – Tseng