簡體   English   中英

使用Server.MapPath()時如何從虛擬路徑中排除控制器名稱?

[英]How can I exclude controller name from the virtual path when I use Server.MapPath()?

在我的MVC應用程序中,我從MyController的action方法調用Server.MapPath()以生成指向存儲在名為Templates的文件夾中的特定文件的虛擬路徑:

Server.MapPath($"Templates/{pdfFileName}")

MapPath()返回如下路徑:

C:\SomePath\MyApp\MyController\Templates\MyFile.pdf

這不好,因為實際路徑應該是

C:\SomePath\MyApp\Templates\MyFile.pdf

如何獲得正確的道路? 謝謝。

嘗試使用~ :例如:

string path = Path.Combine(Server.MapPath(@"~/Templates/" + pdfFileName)); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM