iT邦幫忙

0

.net 6 MVC要怎麼樣才能讀寫IIS設定的虛擬目錄內的檔案

  • 分享至 

  • xImage

最近在把公司中使用.net framework 4.5用WebForm架設的網站升級為使用.NET8 MVC,但碰到了一個問題,就是原本的系統為了把檔案放到FileServer上,所以有在IIS上設定虛擬目錄,但是在.NET8中,取得虛擬目錄實際路徑的函數(Server.MapPath)已經不能再用了,想請問現在.NET8要做什麼設定才能讀寫呢?

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
厚厚
iT邦研究生 5 級 ‧ 2024-05-28 13:37:33

使用虛擬目錄的相對路徑來操作檔案
string relativePath = "~/你的虛擬目錄/你的檔案.txt";
string physicalPath = _webHostEnvironment.WebRootFileProvider.GetFileInfo(relativePath).PhysicalPath;

0
breezy89757
iT邦新手 5 級 ‧ 2024-05-28 18:19:23

Environment.CurrentDirectory可以取得網站執行的實際路徑
https://ithelp.ithome.com.tw/upload/images/20240528/201671583mrAELd6ye.png

我要發表回答

立即登入回答