求跨虚拟目录操作的方法

xu8512 2007-04-29 11:09:10
例如网站位于WebSite虚拟目录下面,图片位于Image虚拟目录下面
要怎么才能使WebSite目录下的网页,访问Image目录下的图片


由于Image文件夹的物理地址不明确,所以WebSite里面网页运行时首先要
获得Image虚拟目录的物理地址,这个该怎么来获取?问题就卡在这里了

尝试通过Server.MapPath("/Image/");来获取,不成功
...全文
249 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
daguzhang 2007-04-29
  • 打赏
  • 举报
回复
你的WEB项目是基于HTTP格式还文件系统的
如果是HTTP格式的 用
Server.MapPath("/Image/");
是可以的
daguzhang 2007-04-29
  • 打赏
  • 举报
回复
你的Image虚拟目录可能被安装到的目录不确定 是把
xu8512 2007-04-29
  • 打赏
  • 举报
回复
forgot(忘记forgot2000
你的答案不是我要问的,还是谢谢了
cflegs 2007-04-29
  • 打赏
  • 举报
回复
目录操作权限的问题,你需要设置Image的NETWORK SERVICE的读写权限(2003操作系统),如果你的操作系统是2000,还需要设置ASP.NET的读写权限。
forgot 2007-04-29
  • 打赏
  • 举报
回复
/// <summary>
/// 网站原始地址 like:http://www.aspxboy.com
/// </summary>
public static string BaseUrl
{
get { return HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority); }
}

/// <summary>
/// 应用程序虚拟路径 like: http://www.aspxboy.com/dir
/// </summary>
public static string SiteUrl
{
get
{

return Globals.BaseUrl + Globals.AppPath;
}
}

/// <summary>
/// 应用程序路径
/// </summary>
public static string AppPath
{

get
{
string applicationPath = "/";

if(HttpContext.Current != null)
{
applicationPath = HttpContext.Current.Request.ApplicationPath;
}

if (applicationPath == "/")
{
return string.Empty;
}
else
{
if (applicationPath.EndsWith("/"))
{
return applicationPath;
}
else
{
return applicationPath + "/";
}
}

}
}
/// <summary>
/// 解析相对路径名称,合法的路径名必须以~,http://,https://开头,例如:输入~/try.aspx,当前虚拟目录为Dir1,返回
/// http://localhost/Dir1/try.aspx,如果输入http://localhost/Dir1/try.aspx,则原样返回
/// </summary>
public static string getFullUrl(string relativeUrl)
{
if (relativeUrl.StartsWith("~"))
return Globals.SiteUrl + relativeUrl.Substring(1);
else if (relativeUrl.StartsWith("http://") || relativeUrl.StartsWith("https://"))
return relativeUrl;
else
throw new ArgumentException("URLs passed to Helpers.ResolveUrl() must begin with ~, http://, or https://");
}
xu8512 2007-04-29
  • 打赏
  • 举报
回复
Server.MapPath("/")
也不行
xu8512 2007-04-29
  • 打赏
  • 举报
回复
楼上没一个正确答案,说了物理路径是完全不固定的,而且是其它虚拟目录的物理路径,不是当前的
daguzhang 2007-04-29
  • 打赏
  • 举报
回复
你的情况就用Server.MapPath("/")
yitian130 2007-04-29
  • 打赏
  • 举报
回复
up
yuzl32 2007-04-29
  • 打赏
  • 举报
回复
直接"/Image/root.gif" 就可以了.
比如:
TestControl.ImageUrl = "/Image/root.gif";
daguzhang 2007-04-29
  • 打赏
  • 举报
回复
2。 得到www路径 (/)=(c:\inetpub\wwwroot\)
filename=Server.MapPath("/msg/ErrorMessage.txt"); //c:\inetpub\wwwroot\msg\ErrorMessage.txt
filename=Server.MapPath("/") +"msg/ErrorMessage.txt";
filename=Server.MapPath("../msg/ErrorMessage.txt");

3。 得到当前虚拟根目录的路径 ( ~/)
filename=Server.MapPath("~/msg/ErrorMessage.txt");//D:\公司项目\PowerMis\Web\msg\ErrorMessage.txt
filename=HttpRuntime.AppDomainAppPath;
4. 当前路径(相对)( ./)
filename=Server.MapPath("./ErrorMessage.txt");D:\公司项目\PowerMis\Web\ ErrorMessage.txt
filename=Server.MapPath(Request.ApplicationPath);
filename=Server.MapPath(".");
dengenming 2007-04-29
  • 打赏
  • 举报
回复
别忘了添加network的目录权限
honkerhero 2007-04-29
  • 打赏
  • 举报
回复
这个东西是不是应该在配置文件中设置啊

想不到好办法
forgot 2007-04-29
  • 打赏
  • 举报
回复
System.Web.HttpContext.Current.Server.MapPath("~/Image/");
forgot 2007-04-29
  • 打赏
  • 举报
回复
System.Web.HttpContext.Current.Server.MapPath("/Image/");
lireng 2007-04-29
  • 打赏
  • 举报
回复
直接指定你的物理目录就OK了嘛!
xu8512 2007-04-29
  • 打赏
  • 举报
回复
获取的是另外一个虚拟目录的物理路径
不是本网页的虚拟路径物理路径
Mirricle 2007-04-29
  • 打赏
  • 举报
回复
("~/Image/");

~是根目录
xu8512 2007-04-29
  • 打赏
  • 举报
回复
http://www.cnblogs.com/TonyJoule/archive/2007/04/03/90585.html#Post
正确答案在这里,谢谢楼上各位了,虽然没一个对的

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧