All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
浅谈同页面用多个id_[Html教程]
用PHP实现文件上传_php资料_编程技术
你必须知道的.NET之class和struct_[Asp.Net教程]
SQL,SERVER的内存会不断增加,问题分析_[SQL,Server教程]
delphi弹出选择对话框选择目录
如何删除SQL Server日志?_[SQL Server教程]
动态网页制作技术PHP处理时间的实例_php资料_编程技术
ASP.Net,2.0,发送邮件的代码_.net资料_编程技术
PHP,内存管理器符号比较多个溢出漏洞_php资料_编程技术
Asp.net+Xml实现无数据库论坛一点即通_[Asp.Net教程]
Reading number is top 10 pictures
The money of more than 100 countries and regions13
支持判处贩卖儿童者死刑
Beauty ZhiHuiLin1
美奂绝伦的风景
乳娘帕梅拉安德森5
Tie a large font of mouse
Take you to walk into the most true north Korea rural3
Female model behind the bitterness, often being overcharged2
Beautiful Japanese beauty(漂亮的日本美女)2
迷人的靓女
Download software ranking
变速齿轮3.26
Proficient in Eclipse
Jinling thirteen stock
都市狐狸姑娘传
SP3 for SQL2000
Red cliff
C#与.NET技术平台实战演练
The Bermuda triangle3
SQL2000 For 4IN1
虚拟机汉化软件
delv published in(发表于) 2014/1/6 9:03:36 Edit(编辑)
ASP.NET将Web站点下的绝对路径转换为虚拟路径_[Asp.Net教程]

ASP.NET将Web站点下的绝对路径转换为虚拟路径_[Asp.Net教程]

ASP.NET将Web站点下的绝对路径转换为虚拟路径_[Asp.Net教程]

很经常使用到的一个功能,但在在网上却一直没有找到相关的解决方法,今天借着项目应用到的机会写了两个将绝对路径转换为虚拟路径封装好的方法
将Web站点下的绝对路径转换为相对于指定页面的虚拟路径
/**////


/// 将Web站点下的绝对路径转换为相对于指定页面的虚拟路径
///

/// 当前页面指针,一般为this
/// 绝对路径
/// 虚拟路径, 型如: ../../
public static string ConvertSpecifiedPathToRelativePathForPage(Page page, string specifiedPath)
{
// 根目录虚拟路径
string virtualPath = page.Request.ApplicationPath;
// 根目录绝对路径
string pathRooted = HostingEnvironment.MapPath(virtualPath);
// 页面虚拟路径
string pageVirtualPath = page.Request.Path;


if (!Path.IsPathRooted(specifiedPath) || specifiedPath.IndexOf(pathRooted) == -1)
{
throw new Exception(string.Format("\"{0}\"是虚拟路径而不是绝对路径!", specifiedPath));
}


// 转换成相对路径
//(测试发现,pathRooted 在 VS2005 自带的服务器跟在IIS下根目录或者虚拟目录运行似乎不一样,
// 有此地方后面会加"\", 有些则不会, 为保险起见判断一下)
if (pathRooted.Substring(pathRooted.Length - 1, 1) == "\\")
{
specifiedPath = specifiedPath.Replace(pathRooted, "/");
}
else
{
specifiedPath = specifiedPath.Replace(pathRooted, "");
}


string relativePath = specifiedPath.Replace("\\", "/");


string[] pageNodes = pageVirtualPath.Split('/');


// 减去最后一个页面和前面一个 "" 值
int pageNodesCount = pageNodes.Length - 2;


for (int i = 0; i < pageNodesCount; i++)
{
relativePath = "/.." + relativePath;
}


if (pageNodesCount > 0)
{
// 如果存在 ".." , 则把最前面的 "/" 去掉
relativePath = relativePath.Substring(1, relativePath.Length - 1);
}


return relativePath;
}


第二个方法显然是从第一个方法中的前部分抽取出来的,所以懒得去添加相关注释 :P
将Web站点下的绝对路径转换为虚拟路径
/**////


/// 将Web站点下的绝对路径转换为虚拟路径
/// 注:非Web站点下的则不转换
///

/// 当前页面指针,一般为this
/// 绝对路径
/// 虚拟路径, 型如: ~/
public static string ConvertSpecifiedPathToRelativePath(Page page, string specifiedPath)
{
string virtualPath = page.Request.ApplicationPath;


string pathRooted = HostingEnvironment.MapPath(virtualPath);


if (!Path.IsPathRooted(specifiedPath) || specifiedPath.IndexOf(pathRooted) == -1)
{
return specifiedPath;
}


if (pathRooted.Substring(pathRooted.Length - 1, 1) == "\\")
{
specifiedPath = specifiedPath.Replace(pathRooted, "~/");
}
else
{
specifiedPath = specifiedPath.Replace(pathRooted, "~");
}


string relativePath = specifiedPath.Replace("\\", "/");
return relativePath;
}


将虚拟路径转绝对路就没什么好说的了, HttpRequest.MapPath 方法专门干这事







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.