网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > ASP.NET技巧
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.如何实现Asp与Asp.Net共享Session.
.为ASP.NET应用缓存Oracle数据.
.asp.net中XML文件作为中间件进行.
.GridViewRow可以任意位置单击引发.
.Asp.net url分页的用户控件 .
.修改asp.net2.0内置成员管理中的.
.如何让.Net控件在设计时Initiali.
.求素数(质数)的算法.
.解决网址参数获取乱码问题.
.ASP.NET实现页面间值传递的几种方.
.Asp.net中多彩下拉框的实现.
.如何防止插入删除表造成的数据库.
.XP安装IIS及Framework后无法访问.
.对.NET Framework 反射的反思.
.js操作listbox.
.用post方法从网上抓取信息.
.轻松加密ASP.NET 2.0 Web程序配置.
.一个ASP.NET中使用的MessageBox类.
..NET正则表达式使用高级技巧之反.
.ASP.NET操作IIS中的虚拟目录.

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

发表日期:2006-5-14


很经常使用到的一个功能,但在在网上却一直没有找到相关的解决方法,今天借着项目应用到的机会写了两个将绝对路径转换为虚拟路径封装好的方法
将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简便使用SCRIPT 人气:6547
下一篇:在atlas里面的UpdatePanel控件中调用javascript,以及updatePanel的一些原理 人气:8498
浏览全部asp.net的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐