在使用MVC3使用Server.Mappath [英] Using Server.MapPath in MVC3
问题描述
我有code
string xsltPath = System.Web.HttpContext.Current.Server.MapPath(@"App_Data") + "\\" + TransformFileName
它返回
C:\\的Inetpub \\ wwwroot的\\ websiteName \\ SERVICENAME \\ App_Data文件\\ FileName.xsl
为什么我收到了路径的ServiceController, SERVICENAME
?我想路径的App_Data这是
Why am I getting the path to the ServiceController, SERVICENAME
? I want the path to App_Data which is in
C:\\的Inetpub \\ wwwroot的\\ websiteName \\ App_Data文件\\ FileName.xsl
推荐答案
您需要指定您希望从虚拟根目录开始:
You need to specify that you want to start from the virtual root:
string xsltPath = Path.Combine(System.Web.HttpContext.Current.Server.MapPath(@"~/App_Data"), TransformFileName);
此外,最好的做法是使用 Path.Combine
为路径结合起来,而不是连接字符串。 Path.Combine
将确保你不会用双路径分隔符的情况下结束了。
Additionally, it's better practice to use Path.Combine
to combine paths rather than concatenate strings. Path.Combine
will make sure you won't end up in a situation with double-path separators.
修改
你能定义绝对和相对路径和如何比较物理和虚拟的路径?
Can you define "absolute" and "relative" paths and how they compare to "physical" and "virtual" paths?
MSDN有很好的解释。看看那里。
MSDN has a good explanation on relative, physical, and virtual paths. Take a look there.
这篇关于在使用MVC3使用Server.Mappath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!