【发布时间】:2011-09-20 03:45:54
【问题描述】:
我想将此物理路径"C:\bla\bla\Content\Upload\image.jpg" 转换为像"/Content/Upload/image.jpg" 这样的服务器路径。
我该怎么做?
【问题讨论】:
-
在哪里?在客户端还是服务器端?
标签: asp.net-mvc asp.net-mvc-3 path relative-path virtual-path
我想将此物理路径"C:\bla\bla\Content\Upload\image.jpg" 转换为像"/Content/Upload/image.jpg" 这样的服务器路径。
我该怎么做?
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-3 path relative-path virtual-path
你可以使用类似的东西:
public static class Extensions {
public static string RelativePath(this HttpServerUtility utility, string path, HttpRequest context)
{
return path.Replace(context.ServerVariables["APPL_PHYSICAL_PATH"], "/").Replace(@"\", "/");
}
}
然后你打电话
Server.RelativePath(path, Request);
【讨论】:
您可以执行以下操作来获取相对路径。
String filePath = @"C:\bla\bla\Content\Upload\image.jpg";
String serverPath = Request.PhysicalPath;
String relativePath = filePath.Substring(serverPath.Length, filePath.Length - serverPath.Length);
【讨论】: