事實上iis是不支持.net core mvc項目虛擬目錄的。你在iis上發布網站 然后在wwwroot目錄上創建虛擬目錄,指向硬盤其他位置上的文件夾,是不會有效果的。
正確的處理方式應該是修改靜態文件中間件
這里直接曬出代碼:
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); } //注意 這是用來處理請求靜態文件的中間件 還有專門針對請求目錄的中間件UseDirectoryBrowser() //推薦文章 https://www.jb51.net/article/99303.htm //使用靜態文件 默認指向wwwroot app.UseStaticFiles(); //再插入一個中間件,這次不使用默認的 app.UseStaticFiles(new StaticFileOptions() { //FileProvider是專門用來讀取靜態文件的 FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"G:\Video")), //如果請求的路徑是以下的這個路徑 就用上面這個FileProvider RequestPath = new PathString("/gVideos") }); app.UseStaticFiles(new StaticFileOptions() { FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"I:\video")), RequestPath = new PathString("/iVideos") }); app.UseStaticFiles(new StaticFileOptions() { FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"J:\video")), RequestPath = new PathString("/jVideos") }); app.UseCookiePolicy(); app.UseMvc(); }
參考的文章:https://www.jb51.net/article/99303.htm 這篇文章非常值得一看
