简体   繁体   English

使用Server.MapPath()时如何从虚拟路径中排除控制器名称?

[英]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: 在我的MVC应用程序中,我从MyController的action方法调用Server.MapPath()以生成指向存储在名为Templates的文件夹中的特定文件的虚拟路径:

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

MapPath() returns a path like this: MapPath()返回如下路径:

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)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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