一 ASP.NET常用路径(path)获取方法与格式对照表

  假设我们的网址为http://localhost:1897/ News/Press/Content.aspx?id=1019

 Browser Request 的网址相关的属性与方法

输出(output)实例

备        

Request.ApplicationPath

/

指的是当前的application(应用程序)的目录

Request.PhysicalPath

D:\Projects\Solution\web\News\Press\Content.aspx

磁盘驱动器代号:\父目录\子目录\Content.aspx

Request.PhysicalApplicationPath

D:\Projects\Solution\web\

磁盘驱动器代号:\父目录\子目录\

Request.CurrentExecutionFilePath

/News/Press/Content.aspx

Request.FilePath

/News/Press/Content.aspx

对应于iis的虚拟目录。

Request.Path

/News/Press/Content.aspx

当前请求的虚拟路径。Path 是 FilePath 和 PathInfo 尾部的串联。*(见下面详细讲解)

Server.MapPath(string url)

例http://www.example.com/1/index.html, 假设你的应用程序在c:/iis/MySite中,那么就是c:/iis/MySite/1/index.html

将url映射为服务器上的物理路径

Request.RawUrl

/News/Press/Content.aspx?id=1019

Request.Url.AbsolutePath

/News/Press /Content.aspx

Request.Url.AbsoluteUri

http://localhost:1897/Content.aspx?id=1019

Request.Url.LocalPath

/News/Press//Content.aspx

Request.Url.PathAndQuery

/News/Press//Content.aspx?id=1019&uu=77

Request.Url.Scheme

http

Request.Url.Host

localhost

Request.Url.Port

1987

Request.Url.Authority

localhost:1897

Request.Url.Query

?id=1019

Request.Url.Query[id]

1019

Request.Url.Fragments

/
News/
Press/
Content.aspx

 

Request.Url.Segments[0]

 

System.IO.Path.GetDirectoryName(Request.PhysicalPath)

D:\Projects\Solution\web\News\Press 

磁盘驱动器代号:\父目录\子目录\ 

 

System.IO.Path.GetFileName(Request.PhysicalPath)

Content.aspx

(接上面*) Request.FilePath, Request.PathInfo, Request.Path, RequestRawUrl

 

  如果请求的地址为http://www.cnblogs.com/default.aspx/books则

Request.FilePath值为http://www.cnblogs.com/default.aspx

Request.PathInfo 值为 /books

Request.Path 值为 http://www.cnblogs.com/default.aspx/books

Request.RawUrl 值为 http://www.cnblogs.com/default.aspx/books

  如果请求地址为http://www.cnblogs.com/defaut.aspx?id=1&name=kk则

Request.FilePath值为http://www.cnblogs.com/default.aspx

Request.PathInfo 值为 ""(空字符串)

Request.Path 值为 http://www.cnblogs.com/default.aspx

Request.RawUrl 值为 http://www.cnblogs.com/default.aspx?id=1&name=kk

  二 Request.ServerVariables集合中获取到的相关信息:

 
  左列是服务器变量名,右侧是值,值是通过Request.ServerVariables[服务器变量名]获取的
APPL_MD_PATH : /LM/W3SVC/894523/Root
APPL_PHYSICAL_PATH : D:\VssWorkFolder\British_School_MIS\src\WebSite\
INSTANCE_META_PATH : /LM/W3SVC/894523
LOCAL_ADDR : 192.168.1.6
PATH_INFO : /SysOption/BillingSetup1.aspx
PATH_TRANSLATED : D:\VssWorkFolder\British_School_MIS\src\WebSite\SysOption\BillingSetup1.aspx
REMOTE_ADDR : 192.168.1.6
REMOTE_HOST : 192.168.1.6
SCRIPT_NAME : /SysOption/BillingSetup1.aspx
SERVER_NAME : 192.168.1.6
URL : /SysOption/BillingSetup1.aspx

  Request.ServerVariables是一个很强大的工具,可以帮助我们获取很多client和web宿主的信息,有兴趣的朋友可以通过以下代码看看它到底包含什么信息

foreach (string s in Request.ServerVariables)
        {
            Response.Write(s + "  :  " + Request.ServerVariables[s] + "<br /><br />");
        }

  三 path转换

 
  1.转换为服务器端路径(Server.MapPath)
web服务器端开发设计一个有趣的问题就是,地址转换。比如http地址/images/a.txt,如果你想在服务器端通过io读取这个文件,就得有这个文件的“本机地址(形如c:\windows\system32\xx.dll)”,这时Server.MapPath就很有用了
Response.Write(Request.MapPath(Request.Path));        输出为 D:\VssWorkFolder\British_School_MIS\src\WebSite\SysOption\BillingSetup1.aspx
2.转换为http地址(Page.ResolveClientUrl Page.ResolveUrl)
Response.Write(Page.ResolveClientUrl("~/a/a.jpg"));      输出为 ../a/a.jpg 
Response.Write(Page.ResolveUrl("~/a/a.jpg"));              输出为 /a/a.jpg

  另外,我们使用upload控件上传文件时,用HttpPostedFile 。例如:

HttpPostedFile file = context.Request.Files[i];//这里的context.Request.Files就是上传的文件集合.

PS:此处乃是利用HttpHandler..在Page页面中可以自己用其它办法多文件上传.

  接着如何保存文件呢?

利用HttpPostedFile的SaveAs方法即可,如: file.SaveAs(SpecifiedPath);

此处的SpecifiedPath是上传文件的绝对路径.

  至于如何获取上传文件的路径.我们可以利用Path类.来操作File.HttpPostedFile类中也包含了文件的基本信息.如文件名,大小,路径等等.Path类操作更齐全而已.接着就可以利用Server.MapPath()方法来进行转换.

  为检验上面的理论,你可以编写一段code跑下就一清二楚啦。例:

StringBuilder req = new StringBuilder();

req.Append("<table cellpadding=3 cellspacing=0 border=1>");

// Request.ApplicationPath

req.Append("<tr><td>");

req.Append("Request.ApplicationPath");

req.Append("</td><td>");

req.Append("<b>" + Request.ApplicationPath + "</b>");

req.Append("</td></tr>");

// Request.PhysicalPath

req.Append("<tr><td>");

req.Append("Request.PhysicalPath");

req.Append("</td><td>");

req.Append("<b>" + Request.PhysicalPath + "</b>");

req.Append("</td></tr>");

// System.IO.Path.GetDirectoryName(Request.PhysicalPath)

req.Append("<tr><td>");

req.Append("System.IO.Path.GetDirectoryName(Request.PhysicalPath)");

req.Append("</td><td>");

req.Append("<b>" + System.IO.Path.GetDirectoryName(Request.PhysicalPath) + "</b>");

req.Append("</td></tr>");

// Request.PhysicalApplicationPath

req.Append("<tr><td>");

req.Append("Request.PhysicalApplicationPath");

req.Append("</td><td>");

req.Append("<b>" + Request.PhysicalApplicationPath + "</b>");

req.Append("</td></tr>");

// System.IO.Path.GetFileName(Request.PhysicalPath)

req.Append("<tr><td>");

req.Append("System.IO.Path.GetFileName(Request.PhysicalPath)");

req.Append("</td><td>");

req.Append("<b>" + System.IO.Path.GetFileName(Request.PhysicalPath) + "</b>");

req.Append("</td></tr>");

// Request.CurrentExecutionFilePath

req.Append("<tr><td>");

req.Append("Request.CurrentExecutionFilePath");

req.Append("</td><td>");

req.Append("<b>" + Request.CurrentExecutionFilePath + "</b>");

req.Append("</td></tr>");

// Request.FilePath

req.Append("<tr><td>");

req.Append("Request.FilePath");

req.Append("</td><td>");

req.Append("<b>" + Request.FilePath + "</b>");

req.Append("</td></tr>");

// Request.Path

req.Append("<tr><td>");

req.Append("Request.Path");

req.Append("</td><td>");

req.Append("<b>" + Request.Path + "</b>");

req.Append("</td></tr>");

// Request.RawUrl

req.Append("<tr><td>");

req.Append("Request.RawUrl");

req.Append("</td><td>");

req.Append("<b>" + Request.RawUrl + "</b>");

req.Append("</td></tr>");

// Request.Url.AbsolutePath

req.Append("<tr><td>");

req.Append("Request.Url.AbsolutePath");

req.Append("</td><td>");

req.Append("<b>" + Request.Url.AbsolutePath + "</b>");

req.Append("</td></tr>");

// Request.Url.AbsoluteUri

req.Append("<tr><td>");

req.Append("Request.Url.AbsoluteUri");

req.Append("</td><td>");

req.Append("<b>" + Request.Url.AbsoluteUri + "</b>");

req.Append("</td></tr>");

// Request.Url.Scheme

req.Append("<tr><td>");

req.Append("Request.Url.Scheme");

req.Append("</td><td>");

req.Append("<b>" + Request.Url.Scheme + "</b>");

req.Append("</td></tr>");

// Request.Url.Host

req.Append("<tr><td>");

req.Append("Request.Url.Host");

req.Append("</td><td>");

req.Append("<b>" + Request.Url.Host + "</b>");

req.Append("</td></tr>");

// Request.Url.Port

req.Append("<tr><td>");

req.Append("Request.Url.Port");

req.Append("</td><td>");

req.Append("<b>" + Request.Url.Port + "</b>");

req.Append("</td></tr>");

// Request.Url.Authority

req.Append("<tr><td>");

req.Append("Request.Url.Authority");

req.Append("</td><td>");

req.Append("<b>" + Request.Url.Authority + "</b>");

req.Append("</td></tr>");

// local Request.Url.LocalPath

req.Append("<tr><td>");

req.Append("Request.Url.LocalPath");

req.Append("</td><td>");

req.Append("<b>" + Request.Url.LocalPath + "</b>");

req.Append("</td></tr>");

// Request.PathInfo

req.Append("<tr><td>");

req.Append("Request.PathInfo");

req.Append("</td><td>");

req.Append("<b>" + Request.PathInfo + "</b>");

req.Append("</td></tr>");

// Request.Url.PathAndQuery

req.Append("<tr><td>");

req.Append("Request.Url.PathAndQuery");

req.Append("</td><td>");

req.Append("<b>" + Request.Url.PathAndQuery + "</b>");

req.Append("</td></tr>");

// Request.Url.Query

req.Append("<tr><td>");

req.Append("Request.Url.Query");

req.Append("</td><td>");

req.Append("<b>" + Request.Url.Query + "</b>");

req.Append("</td></tr>");

// Request.Url.Fragment

// 原则上你应该无法从 Request.Url.Fragment 取得任何数据,因为通常 Browser 不会送出 #toc 这个部分

req.Append("<tr><td>");

req.Append("Request.Url.Fragment");

req.Append("</td><td>");

req.Append("<b>" + Request.Url.Fragment + "</b>");

req.Append("</td></tr>");

// Request.Url.Segments

req.Append("<tr>");

req.Append("<td>");

req.Append("Request.Url.Segments");

req.Append("</td>");

req.Append("<td>");

string[] segments = Request.Url.Segments;

foreach (string s in segments)

{

req.Append("<b>" + s + "</b>");

req.Append("<p>");

}

req.Append("</td>");

req.Append("</tr>");

req.Append("</table>");

Response.Write(req.ToString());

参考的文章:
http://blog.miniasp.com/post/2008/02/10/How-Do-I-Get-Paths-and-URL-fragments-from-the-HttpRequest-object.aspx

http://www.cnblogs.com/zyip/archive/2009/08/13/1544968.html

如有错误,请不吝指出。

另加上一个实例:

// Builds an absolute URL
  private static string BuildAbsolute(string relativeUri)
  {
    // get current uri
    Uri uri = HttpContext.Current.Request.Url;
    // build absolute path
    string app = HttpContext.Current.Request.ApplicationPath;
    if (!app.EndsWith("/")) app += "/";
    relativeUri = relativeUri.TrimStart('/');
    // return the absolute path
    return HttpUtility.UrlPathEncode(
      String.Format("http://{0}:{1}{2}{3}",
      uri.Host, uri.Port, app, relativeUri));
  }

ASP.NET中的路径(path) 详解的更多相关文章

  1. Asp.net中web.config配置文件详解(一)

    本文摘自Asp.net中web.config配置文件详解 web.config是一个XML文件,用来储存Asp.NET Web应用程序的配置信息,包括数据库连接字符.身份安全验证等,可以出现在Asp. ...

  2. ASP.NET 中 OutputCache 指令参数详解

    使用@ OutputCache指令使用@ OutputCache指令,能够实现对页面输出缓存的一般性需要.@ OutputCache指令在ASP.NET页或者页中包含的用户控件的头部声明.这种方式非常 ...

  3. 转载 asp.net中ViewState的用法详解

    转载原地址: http://www.jb51.net/article/73662.htm 在web窗体控件设置为runat = "server",这个控件会被附加一个隐藏的属性_V ...

  4. Asp.net中web.config配置文件详解(二)

    摘自http://blog.csdn.net/hbqhdlc/article/details/8155668 近日正在看Asp.net,看到Web.config有很不清楚之处,特意从网络.MSDN搜集 ...

  5. Asp.net中web.config配置文件详解

    Web.config文件是一个XML文本文件,它用来储存 ASP.NET Web 应用程序的配置信息(如最常用的设置ASP.NET Web 应用程序的身份验证方式),它可以出现在应用程序的每一个目录中 ...

  6. Eclipse中的build path详解

    http://blog.csdn.net/qqqqqq654/article/details/53043742

  7. VS2010 Chart控件(一)Chart控件在ASP.NET网站中的应用示例详解(C#语言)

    步骤如下: 1. Chart控件(一)Chart控件在ASP.NET网站中的应用示例详解(C#语言)" title="VS2010 Chart控件(一)Chart控件在ASP.NE ...

  8. C#中web.config文件详解

    C#中web.config文件详解 一.认识Web.config文件 Web.config 文件是一个XML文本文件,它用来储存 ASP.NET Web 应用程序的配置信息(如最常用的设置ASP.NE ...

  9. Oracle Statspack报告中各项指标含义详解~~学习性能必看!!!

    Oracle Statspack报告中各项指标含义详解~~学习性能必看!!! Data Buffer Hit Ratio#<#90# 数据块在数据缓冲区中的命中率,通常应该在90%以上,否则考虑 ...

随机推荐

  1. 【Unity3d】【项目学习心得】从资源server下载资源(一)

    项目里面的很多资源都是从资源server载入的,这样子能够减小client的包大小. 所以我们须要一个专门的类来管理下载资源. 资源分非常多类型,如:json表,txt文件,image文件,二进制文件 ...

  2. Animate.css 教程

    animate.css 是一个有趣,酷炫的,跨浏览器的动画库,你可以将它用于你的项目中.不管是主页,滑动切换,又或者是其它方面,你都可以通过它来制作出惊人的效果. 基本用法 引入CSS文件 这个对你来 ...

  3. CentOS 7.0 systemd代替service

    CentOS 7.0中一个最主要的改变,就是切换到了systemd.它用于替代红帽企业版Linux前任版本中的SysV和Upstart,对系统和服务进行管理.systemd兼容SysV和Linux标准 ...

  4. uploadify按钮中文乱码问题

    uploadify是一款基于jQuery库的上传插件,但很可惜的是无论你怎么设置参数buttonText ,它的中文按钮都会出现乱码的情况,现把出现原因及解决方法总结如下.       那么出现这种的 ...

  5. 「前端开发者」如何把握住「微信小程序」这波红利?

    由于前两周一直在老家处理重要事情,虽然朋友圈被「微信小程序」刷爆了,但并没有时间深入了解. 昨天回广州之后,第一件事情就是把「微信小程序」相关的文章.开发文档.设计规范全部看了一遍,基本上明白了「微信 ...

  6. DevExpress中SearchLookUpEdit用法总结

    在前一个项目中用到了DevExpress,需要搜索某一个字段,来拉取出对应的相关信息,比来比去,发现SearchLookUpEdit的用户体验更好,但自己是个不折不扣的C#和DevExpress的初学 ...

  7. 沃通tomcat jks 安装配置

    废话不多说上代码: <Connector port="443" protocol="org.apache.coyote.http11.Http11NioProtoc ...

  8. JSP 基础之 JSTL <c:forEach>用法

    在JSP的开发中,迭代是经常要使用到的操作.例如,逐行的显示查询的结果等.在早期的JSP中,通常使用Scriptlets来实现Iterator或者Enumeration对象的迭代输出.现在,通过JST ...

  9. linux学习笔记之IO

    一.基础知识. 1:普通IO类型. 1,非阻塞IO:发出open/read/write等IO操作,并使这些操作不会永远阻塞.当不能完成时,会立即出错返回. 1)非阻塞的两种标志方式:指定标志:O_NO ...

  10. Java三大特征之继承(二)

    在<Think in java>中有这样一句话:复用代码是Java众多引人注目的功能之一.但要想成为极具革命性的语言,仅仅能够复制代码并对加以改变是不够的,它还必须能够做更多的事情.在这句 ...