ASP.NET将Web站点下的绝对路径转换为虚拟路径
来源:互联网 发布:2017悬疑推理网络剧 编辑:程序博客网 时间:2024/05/16 12:47
ASP.NET将Web站点下的绝对路径转换为虚拟路径
2009-11-25 10:39
很经常使用到的一个功能,但在在网上却一直没有找到相关的解决方法,今天借着项目应用到的机会写了两个将绝对路径转换为虚拟路径封装好的方法
将Web站点下的绝对路径转换为相对于指定页面的虚拟路径
/**//// <summary>
/// 将Web站点下的绝对路径转换为相对于指定页面的虚拟路径
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="specifiedPath">绝对路径</param>
/// <returns>虚拟路径, 型如: ../../</returns>
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站点下的绝对路径转换为虚拟路径
/**//// <summary>
/// 将Web站点下的绝对路径转换为虚拟路径
/// 注:非Web站点下的则不转换
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="specifiedPath">绝对路径</param>
/// <returns>虚拟路径, 型如: ~/</returns>
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 方法专门干这事
将Web站点下的绝对路径转换为相对于指定页面的虚拟路径
/**//// <summary>
/// 将Web站点下的绝对路径转换为相对于指定页面的虚拟路径
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="specifiedPath">绝对路径</param>
/// <returns>虚拟路径, 型如: ../../</returns>
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站点下的绝对路径转换为虚拟路径
/**//// <summary>
/// 将Web站点下的绝对路径转换为虚拟路径
/// 注:非Web站点下的则不转换
/// </summary>
/// <param name="page">当前页面指针,一般为this</param>
/// <param name="specifiedPath">绝对路径</param>
/// <returns>虚拟路径, 型如: ~/</returns>
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 方法专门干这事
- ASP.NET将Web站点下的绝对路径转换为虚拟路径
- 将Web站点下的绝对路径转换为虚拟路径
- 将Web站点下的绝对路径转换为虚拟路径
- 将Web站点下的绝对路径转换为虚拟路径
- ASP.NET根路径的获取和将Web站点下的绝对路径转换为虚拟路径的两种方案
- ASP.net 1.1 中相对路径转换为绝对路径
- C# 将绝对路径转换为相对路径
- Asp相对路径转换为绝对路径函数
- .net下的ueditor更改上传路径为绝对路径
- ASP.NET下获取站点各种路径
- 使用 JavaScript 将相对路径转换为绝对路径
- PHP将相对路径URL转换为绝对路径URL
- asp.net服务器绝对路径转换成URL相对路径
- asp.net(c#)中相对路径(虚拟路径)和物理磁盘路径的转换
- 相对路径转换为绝对路径
- 绝对路径转换为相对路径
- 将 ASP 转换为 ASP.NET
- Asp.net 当中的虚拟路径
- pz伯伯的番茄排序
- map的一种替代方案,不会自动排序
- 使用truss、strace或ltrace诊断软件问题
- ASP.NET - XML Web 服务器控件概述
- 文件上传功能
- ASP.NET将Web站点下的绝对路径转换为虚拟路径
- 解决IE下图片渐隐渐现效果,图片出现“坏点”的bug
- ASP.NET(C#)通用数据库类
- 掷去欺辱 自取其辱 智取其乳
- Base加解密
- 理解BinFS, Multi-XIP, Multi-bin
- 使用方便的纯js图表插件:JScharts
- Delphi制作防调试程序,19种反调试检测法
- 韩香道有市场并非纸上谈兵