2016-08-17 47 views
-3

我有一個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

期待您的幫助。

+0

你可以粘貼API代碼也? –

+0

你不發表您的中間件配置,如果沒有它,我們註定要猜 – Tseng

回答

-1

這是我固定的問題,我沒有讓我的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") 
     }); 
} 

享受!