将Web站点下的绝对路径转换为虚拟路径

很经常使用到的一个功能,但在在网上却一直没有找到相关的解决方法,今天借着项目应用到的机会写了两个将绝对路径转换为虚拟路径封装好的方法
ContractedBlock.gif ExpandedBlockStart.gif 将Web站点下的绝对路径转换为相对于指定页面的虚拟路径
ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
InBlock.gif
/// 将Web站点下的绝对路径转换为相对于指定页面的虚拟路径
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="page">当前页面指针,一般为this</param>
InBlock.gif
/// <param name="specifiedPath">绝对路径</param>
ExpandedBlockEnd.gif
/// <returns>虚拟路径, 型如: ../../</returns>

None.gifpublic static string ConvertSpecifiedPathToRelativePathForPage(Page page, string specifiedPath)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
// 根目录虚拟路径
InBlock.gif
    string virtualPath = page.Request.ApplicationPath;
InBlock.gif    
// 根目录绝对路径
InBlock.gif
    string pathRooted = HostingEnvironment.MapPath(virtualPath);
InBlock.gif    
// 页面虚拟路径
InBlock.gif
    string pageVirtualPath = page.Request.Path;
InBlock.gif
InBlock.gif    
if (!Path.IsPathRooted(specifiedPath) || specifiedPath.IndexOf(pathRooted) == -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
throw new Exception(string.Format("\"dot.gif{0}\"是虚拟路径而不是绝对路径!", specifiedPath));
ExpandedSubBlockEnd.gif    }

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

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        specifiedPath 
= specifiedPath.Replace(pathRooted, "");
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
string relativePath = specifiedPath.Replace("\\""/");
InBlock.gif
InBlock.gif    
string[] pageNodes = pageVirtualPath.Split('/');
InBlock.gif
InBlock.gif    
// 减去最后一个页面和前面一个 "" 值
InBlock.gif
    int pageNodesCount = pageNodes.Length - 2;
InBlock.gif
InBlock.gif    
for (int i = 0; i < pageNodesCount; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        relativePath 
= "/.." + relativePath;
ExpandedSubBlockEnd.gif    }

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

InBlock.gif
InBlock.gif    
return relativePath;
ExpandedBlockEnd.gif}


第二个方法显然是从第一个方法中的前部分抽取出来的,所以懒得去添加相关注释 :P
ContractedBlock.gif ExpandedBlockStart.gif 将Web站点下的绝对路径转换为虚拟路径
ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
InBlock.gif
/// 将Web站点下的绝对路径转换为虚拟路径
InBlock.gif
/// 注:非Web站点下的则不转换
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="specifiedPath">绝对路径</param>
ExpandedBlockEnd.gif
/// <returns>虚拟路径, 型如: ~/</returns>

None.gifpublic static string ConvertSpecifiedPathToRelativePath(string specifiedPath)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
string pathRooted = HostingEnvironment.MapPath("~/");
InBlock.gif
InBlock.gif    
if (!Path.IsPathRooted(specifiedPath) || specifiedPath.IndexOf(pathRooted) == -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return specifiedPath;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
if (pathRooted.Substring(pathRooted.Length - 11== "\\")
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        specifiedPath 
= specifiedPath.Replace(pathRooted, "~/");
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        specifiedPath 
= specifiedPath.Replace(pathRooted, "~");
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
string relativePath = specifiedPath.Replace("\\""/");
InBlock.gif    
return relativePath;
ExpandedBlockEnd.gif}


将虚拟路径转绝对路就没什么好说的了, HttpRequest.MapPath 方法专门干这事
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值