简体   繁体   中英

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

In my MVC application, I call Server.MapPath() from MyController's action method to generate a virtual path to a particular file stored in a folder called Templates:

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

MapPath() returns a path like this:

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

This is not good, as the actual path should be

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

How to get the right path? Thanks.

尝试使用~ :例如:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM