(精華)2020年7月1日 ASP.NET Core 使用靜態檔案和目錄瀏覽

愚公搬程式碼發表於2020-07-01
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
    #region 使用靜態檔案和目錄瀏覽
    app.UseStaticFiles(new StaticFileOptions
    {
         FileProvider = new PhysicalFileProvider("D:/MyStaticFiles"),//檔案路徑,一定是絕對路徑
         RequestPath = "/StaticFiles",//請求的路徑,一定是絕對路徑 /
         ServeUnknownFileTypes = true,//支援未知的檔案型別
         DefaultContentType = "application/octet-stream"//預設的檔案編碼
     })
     .UseDirectoryBrowser(new DirectoryBrowserOptions
     {
         FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "files")),
         RequestPath = new Microsoft.AspNetCore.Http.PathString("/file")
     })
     .UseFileServer(true)//融合靜態檔案和目錄
     #endregion
}

相關文章