码迷,mamicode.com
首页 > Web开发 > 详细

Asp.Net 获取物理路径

时间:2017-09-09 11:55:51      阅读:333      评论:0      收藏:0      [点我收藏+]

标签:file   方式   map   pathinfo   ase   开发   code   host   href   

一、AppDomain

1.AppDomin获取当前前程序域目录

2.不需要请求上线文实例,例如在Global.ascx中访问等

//网站物理目录
AppDomain.CurrentDomain.BaseDirectory

//网站动态目录
AppDomain.CurrentDomain.DynamicDirectory

结果:

网站物理目录: E:\自开发控件\Pager\PageBar
网站动态目录: C:\Users\tianma\AppData\Local\Temp\Temporary ASP.NET Files\vs\7d482ba1\5e597c25

二、Request.Mapth() / Server.Mappath()

比较常用的是使用MapPath() 方法

1.Server.Mapth() 示例

<p>
    网站物理目录:   <%=Server.MapPath("~") %>
</p>
<p>
    请求文件夹目录:   <%=Server.MapPath(".") %>
</p>
    <p>
    网站物理目录:   <%=Server.MapPath("..") %>
</p>
<p>
    文件目录:   <%=Server.MapPath("~/view/view1.html") %>
</p>

结果:

网站物理目录: E:\自开发控件\Pager\PageBar
请求文件夹目录: E:\自开发控件\Pager\PageBar\View

网站物理目录: E:\自开发控件\Pager\PageBar

文件目录: E:\自开发控件\Pager\PageBar\view\view1.html

 

 

2.Request.Mapth() 使用方式同上,但是Request有更多扩展方便路径访问

<p>
    当前网站物理路径:   <%=Request.PhysicalApplicationPath %>
</p>
<p>
    当前请求文件物理路径:   <%=Request.PhysicalPath %>
</p>

结果:

当前网站物理路径: E:\自开发控件\Pager\PageBar
当前请求文件物理路径: E:\自开发控件\Pager\PageBar\View\directory1.aspx

 

3.Request 访问获取虚拟路径和URL信息

// 获取服务器上 ASP.NET 应用程序的虚拟应用程序根路径:
Request.ApplicationPath;    // /aspnet

// 获取应用程序根的虚拟路径,并通过对应用程序根使用波形符 (~) 表示法使该路径成为相对路径。
Request.AppRelativeCurrentExecutionFilePath;   // ~/zz/zz.aspx

// 获取当前请求的虚拟路径
Request.CurrentExecutionFilePath;    // /aspnet/zz/zz.aspx
Request.FilePath;    // /aspnet/zz/zz.aspx

// 获取CurrentExecutionFilePath属性中指定的文件名的扩展名。
Request.CurrentExecutionFilePathExtension;    // .aspx 

// 获取当前请求的虚拟路径(包括附件路径信息)
Request.Path;    // /aspnet/zz/zz.aspx/info

// 获取具有 URL 扩展名的资源的附加路径信息。
Request.PathInfo;    // /info

// 获取有关当前请求的 URL 的信息。
Request.Url;    // http://localhost/aspnet/zz/zz.aspx/inf?name=wk

// 获取当前请求的原始 URL
Request.RawUrl;    // /aspnet/zz/zz.aspx/inf?name=wk

// 获取有关客户端上次请求的 URL 的信息,该请求链接到当前的 URL。
Request.UrlReferrer;    // System.Uri 

 

更多:

Asp.Net HttpApplication请求管道与Session(二)

Asp.Net HttpApplication 事件汇总

Global.asax使用2

Asp.Net 获取物理路径

标签:file   方式   map   pathinfo   ase   开发   code   host   href   

原文地址:http://www.cnblogs.com/tianma3798/p/7497370.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!