物理路径和虚拟路径 的访问

所有的config 和 应用程序中的路径都是 用 正斜杠 / (不是文件路径的反斜杆 \: C:\Users\)

//Request.CurrentExecutionFilePath; 当前请求的虚拟路径 是这种格式:\虚拟目录名\admin\index.aspx
//Request.ApplicationPath    //获取服务器上 ASP.NET 应用程序的虚拟应用程序根路径。\虚拟目录名(应用程序名)

以上都是虚拟目录中的文件路径,即是IIS中的路径。要得到真真的物理路径要在得到以上路径的基础上再加:
Server.MapPath(Request.ApplicationPath   +"SPLConfig/ClassMap.xml")//这样可能会有一个BUG

最好是这样
string m_ApplicationPath=Request.ApplicationPath;
   if(this.m_ApplicationPath=="")
    this.m_ApplicationPath="/";
   if(!this.m_ApplicationPath.EndsWith("/"))
    this.m_ApplicationPath +="/";  //先判断一下 /
Server.MapPath(m_ApplicationPath+"SPLConfig/ClassMap.xml")

用下面的替换

VirtualPathUtility.AppendTrailingSlash(Request.ApplicationPath) + "SPLConfig/ClassMap.xml"


2005 地址Url 的新的访问方式
//this.Request.CurrentExecutionFilePath; //当前请求的虚拟忙碌中的路径
    //this.Request.FilePath;  //虚拟目录中的路径
    //this.Request.PhysicalPath;  //当前请求的物理路径
    //this.Request.PhysicalApplicationPath;//应用程序的物理路径
    //this.Request.Url.AbsoluteUri;//全部URL

Request.RawUrl 获得网址带变量 Request.UrlReferrer 获得网址不带变量

刷新本页面的方法 :
this.Response.Redirect(this.Request.Url.AbsoluteUri, true);

this.Response.Redirect(this.Request.Url.ToString());

定制方法的刷新

this.Response.Redirect(this.Request.CurrentExecutionFilePath + "?depid=" + this.hDepID.Value.ToString());

.Net中类VirtualPathUtility 的使用

类VirtualPathUtility为常见的虚拟路径操作提供实用工具方法

AppendTrailingSlash 将正斜杠 (/) 追加到虚拟路径的末尾(如果尚不存在正斜杠)。
Combine 将一个基路径和一个相对路径进行组合。
GetDirectory 返回虚拟路径的目录部分。
GetExtension 检索虚拟路径中引用的文件的扩展名。
GetFileName 检索虚拟路径中引用的文件的文件名。
IsAbsolute 返回一个布尔值,该值指示指定的虚拟路径是否为绝对路径,也就是以正斜杠 (/) 开头。
IsAppRelative 返回一个布尔值,该值指示指定的虚拟路径是否为相对于应用程序的相对路径。
MakeRelative 返回从一个包含根操作符(代字号 [~])的虚拟路径到另一个此类虚拟路径的相对虚拟路径。
RemoveTrailingSlash 从虚拟路径去除末尾的正斜杠 (/)。
ToAbsolute 已重载。 将虚拟路径转换为应用程序绝对路径。
ToAppRelative 已重载。 将虚拟路径转换为应用程序相对路径

*******以下为将测试页面放在网站根目录下访问的结果********************************************************

Current file path = "/Default.aspx"
File name = Default.aspx
File extension = .aspx
Directory = /

------------------------------------------------------------------------------------

Current File Path = "/Test1/Default.aspx"
Is Absolute = True
Is AppRelative = False

------------------------------------------------------------------------------------

Current File Path = "/Default.aspx"
ToAbsolute = /Default.aspx
ToAppRelative = ~/Default.aspx

------------------------------------------------------------------------------------

Current Executing File Path = "/Test1/News"
RemoveTrailingSlash = /Test1/News
AppendTrailingSlash = /Test1/News/

*******以下为将测试页面放在网站虚拟目录(Tester1)下访问的结果********************************************************

Current file path = "/Tester1/Default.aspx"
File name = Default.aspx
File extension = .aspx
Directory = /Tester1/

------------------------------------------------------------------------------------

Current File Path = "/Test1/Default.aspx"
Is Absolute = True
Is AppRelative = False

------------------------------------------------------------------------------------

Current File Path = "/Default.aspx"
ToAbsolute = /Default.aspx
ToAppRelative = /Default.aspx

------------------------------------------------------------------------------------

Current Executing File Path = "/Test1/News"
RemoveTrailingSlash = /Test1/News
AppendTrailingSlash = /Test1/News/

需要说明的是,当在虚拟目录下测试的时候,发现 VirtualPathUtility.ToAppRelative(string str)和VirtualPathUtility.IsAppRelative(string str)的测试结果是矛盾的。

比如:

string strpath = "/Default.aspx";

VirtualPathUtility.IsAppRelative(strpath ) 的值为false,但是VirtualPathUtility.ToAppRelative(strpath)的结果却为“/Default.aspx”,而不是想象中的“~/Default.aspx”。

但是,如果将程序放在网站根目录下测试,则结果刚好相反。

不知道这是不是.Net的一个BUG。

所以,目前建议,慎用 VirtualPathUtility.ToAppRelative(string str)。

 https://files.cnblogs.com/anan/Request.rar


 

原文地址:https://www.cnblogs.com/anan/p/372914.html