首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

求跨虚拟目录操作的方法,该怎么处理

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

求跨虚拟目录操作的方法
例如网站位于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)


{
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:// ");
}

[解决办法]
目录操作权限的问题,你需要设置Image的NETWORK SERVICE的读写权限(2003操作系统),如果你的操作系统是2000,还需要设置ASP.NET的读写权限。
[解决办法]
你的WEB项目是基于HTTP格式还文件系统的
如果是HTTP格式的 用
Server.MapPath( "/Image/ ");
是可以的

热点排行