附加信息:'M:/ uploads /'是​​物理路径,但预计会有虚拟路径

时间:2013-12-16 15:34:41

标签: c# asp.net asp.net-mvc

我需要将上传的文件放到我们可以从服务器访问的公共驱动器上。但它正在提供Additional information: 'M:/uploads/' is a physical path, but a virtual path was expected.

 <appSettings>
    <add key="DirPath" value="M:/uploads/"/>
  </appSettings>

代码

String fileName = Path.GetFileNameWithoutExtension(file.FileName);
 String extension = Path.GetExtension(file.FileName);
 var directory = Server.MapPath(@ConfigurationManager.AppSettings["DirPath"]
                                    .ToString(CultureInfo.InvariantCulture));

如何访问该路径?

1 个答案:

答案 0 :(得分:0)

由于异常明确指出你不能(实际上你不需要)使用MapPath作为绝对路径。如果要在配置中支持绝对路径,则只需检查该值以查看路径是否为绝对路径:

 string directory = @ConfigurationManager.AppSettings["DirPath"]
                                         .ToString(CultureInfo.InvariantCulture)
 if(!(path.Contains(":") || path.Contains(@"\\") || path.Contains(@"//"))
    // convert virtual to physical path
    directory = Server.MapPath();
 else