Asp.Net 获取物理路径

一、AppDomain

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

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

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

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

结果:

网站物理目录: E:自开发控件PagerPageBar

网站动态目录: C:Users	ianmaAppDataLocalTempTemporary ASP.NET Filesvs7d482ba15e597c25

二、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:自开发控件PagerPageBar

请求文件夹目录: E:自开发控件PagerPageBarView

网站物理目录: E:自开发控件PagerPageBar

文件目录: E:自开发控件PagerPageBarviewview1.html

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

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

结果:

当前网站物理路径: E:自开发控件PagerPageBar

当前请求文件物理路径: E:自开发控件PagerPageBarViewdirectory1.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

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