日期:2014-05-19  浏览次数:20342 次

求跨虚拟目录操作的方法
例如网站位于WebSite虚拟目录下面,图片位于Image虚拟目录下面
要怎么才能使WebSite目录下的网页,访问Image目录下的图片


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

尝试通过Server.MapPath( "/Image/ ");来获取,不成功

------解决方案--------------------
( "~/Image/ ");

~是根目录
------解决方案--------------------
直接指定你的物理目录就OK了嘛!
------解决方案--------------------
System.Web.HttpContext.Current.Server.MapPath( "/Image/ ");

------解决方案--------------------
System.Web.HttpContext.Current.Server.MapPath( "~/Image/ ");
------解决方案--------------------
这个东西是不是应该在配置文件中设置啊

想不到好办法
------解决方案--------------------
别忘了添加network的目录权限
------解决方案--------------------
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( ". ");

------解决方案--------------------
直接 "/Image/root.gif " 就可以了.
比如:
TestControl.ImageUrl = "/Image/root.gif ";

------解决方案--------------------
你的情况就用Server.MapPath( "/ ")
------解决方案--------------------
/// <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)