原文标题:How Do I Get Paths and URL fragments from the HttpRequest object?
原文地址:摘要
如果你编写过许多ASP.NET程序,那么你可能花了很多时间将Url映射到物理磁盘上的某个位置。当你需要在服务器上保存文件,或者执行某些URL解析、或者将请求重新路由的时候,你都需要做这些工作。
这篇文章将要解释Request对象的一些属性,这些属性根据应用程序和当前请求来提供路径和url信息。
首先,这里有两张表来展示针对给出的请求路径,Request对象的相关属性的值。
由于某些我也不清楚的原因,Url.Fragment属性通常是空的,而不是包含"#fragment"的值。
请求路径: http://localhost:96/Cambia3/Temp/Test.aspx?q=item#fragment
Request.ApplicationPath: | /Cambia3 |
Request.CurrentExecutionFilePath: | /Cambia3/Temp/Test.aspx |
Request.FilePath: | /Cambia3/Temp/Test.aspx |
Request.Path: | /Cambia3/Temp/Test.aspx |
Request.PathInfo: | |
Request.PhysicalApplicationPath: | D:\Inetpub\wwwroot\CambiaWeb\Cambia3\ |
Request.RawUrl: | /Cambia3/Temp/Test.aspx?query=arg |
Request.Url.AbsolutePath: | /Cambia3/Temp/Test.aspx |
Request.Url.AbsoluteUri: | http://localhost:96/Cambia3/Temp/Test.aspx?query=arg |
Request.Url.Fragment: | |
Request.Url.Host: | localhost |
Request.Url.Authority: | localhost:96 |
Request.Url.LocalPath: | /Cambia3/Temp/Test.aspx |
Request.Url.PathAndQuery: | /Cambia3/Temp/Test.aspx?query=arg |
Request.Url.Port: | 96 |
Request.Url.Query: | ?query=arg |
Request.Url.Scheme: | http |
Request.Url.Segments: | / Cambia3/ Temp/ Test.aspx |
请求的路径: http://localhost:96/Cambia3/Temp/Test.aspx/path/info?q=item#fragment
Request.ApplicationPath: | /Cambia3 |
Request.CurrentExecutionFilePath: | /Cambia3/Temp/Test.aspx |
Request.FilePath: | /Cambia3/Temp/Test.aspx |
Request.Path: | /Cambia3/Temp/Test.aspx/path/info |
Request.PathInfo: | /path/info |
Request.PhysicalApplicationPath: | D:\Inetpub\wwwroot\CambiaWeb\Cambia3\ |
Request.RawUrl: | /Cambia3/Temp/Test.aspx/path/info?query=arg |
Request.Url.AbsolutePath: | /Cambia3/Temp/Test.aspx/path/info |
Request.Url.AbsoluteUri: | http://localhost:96/Cambia3/Temp/Test.aspx/path/info?query=arg |
Request.Url.Fragment: | |
Request.Url.Host: | localhost |
Request.Url.LocalPath: | /Cambia3/Temp/Test.aspx/path/info |
Request.Url.PathAndQuery: | /Cambia3/Temp/Test.aspx/path/info?query=arg |
Request.Url.Port: | 96 |
Request.Url.Query: | ?query=arg |
Request.Url.Scheme: | http |
Request.Url.Segments: | / Cambia3/ Temp/ Test.aspx/ path/ info |
下面是我用C#写的一个方法,用来处理URL并且生成表格。你需要在aspx页面中加入一个ID为"lblOutput"的Label控件,然后使用这个方法。
1 private void DisplayRequestObjectProperties() 2 { 3 lblOutput.Text = "
"; 6 lblOutput.Text += "Some HttpRequest path and ULR properties:"; 7 lblOutput.Text += " | |
"; 11 lblOutput.Text += "Request.ApplicationPath:"; 12 lblOutput.Text += " | "; 13 lblOutput.Text += "" + Request.ApplicationPath + ""; 14 lblOutput.Text += " |
"; 18 lblOutput.Text += "Request.CurrentExecutionFilePath:"; 19 lblOutput.Text += " | "; 20 lblOutput.Text += "" + Request.CurrentExecutionFilePath + ""; 21 lblOutput.Text += " |
"; 25 lblOutput.Text += "Request.FilePath:"; 26 lblOutput.Text += " | "; 27 lblOutput.Text += "" + Request.FilePath + ""; 28 lblOutput.Text += " |
"; 32 lblOutput.Text += "Request.Path:"; 33 lblOutput.Text += " | "; 34 lblOutput.Text += "" + Request.Path + ""; 35 lblOutput.Text += " |
"; 39 lblOutput.Text += "Request.PathInfo:"; 40 lblOutput.Text += " | "; 41 lblOutput.Text += "" + Request.PathInfo + ""; 42 lblOutput.Text += " |
"; 46 lblOutput.Text += "Request.PhysicalApplicationPath:"; 47 lblOutput.Text += " | "; 48 lblOutput.Text += "" + Request.PhysicalApplicationPath + ""; 49 lblOutput.Text += " |
"; 53 lblOutput.Text += "Request.RawUrl:"; 54 lblOutput.Text += " | "; 55 lblOutput.Text += "" + Request.RawUrl + ""; 56 lblOutput.Text += " |
"; 60 lblOutput.Text += "Request.Url.AbsolutePath:"; 61 lblOutput.Text += " | "; 62 lblOutput.Text += "" + Request.Url.AbsolutePath + ""; 63 lblOutput.Text += " |
"; 67 lblOutput.Text += "Request.Url.AbsoluteUri:"; 68 lblOutput.Text += " | "; 69 lblOutput.Text += "" + Request.Url.AbsoluteUri + ""; 70 lblOutput.Text += " |
"; 74 lblOutput.Text += "Request.Url.Fragment:"; 75 lblOutput.Text += " | "; 76 lblOutput.Text += "" + Request.Url.Fragment + ""; 77 lblOutput.Text += " |
"; 81 lblOutput.Text += "Request.Url.Host:"; 82 lblOutput.Text += " | "; 83 lblOutput.Text += "" + Request.Url.Host + ""; 84 lblOutput.Text += " |
"; 88 lblOutput.Text += "Request.Url.Authority:"; 89 lblOutput.Text += " | "; 90 lblOutput.Text += "" + Request.Url.Authority + ""; 91 lblOutput.Text += " |
"; 95 lblOutput.Text += "Request.Url.LocalPath:"; 96 lblOutput.Text += " | "; 97 lblOutput.Text += "" + Request.Url.LocalPath + ""; 98 lblOutput.Text += " |
"; 102 lblOutput.Text += "Request.Url.PathAndQuery:"; 103 lblOutput.Text += " | "; 104 lblOutput.Text += "" + Request.Url.PathAndQuery + ""; 105 lblOutput.Text += " |
"; 109 lblOutput.Text += "Request.Url.Port:"; 110 lblOutput.Text += " | "; 111 lblOutput.Text += "" + Request.Url.Port + ""; 112 lblOutput.Text += " |
"; 116 lblOutput.Text += "Request.Url.Query:"; 117 lblOutput.Text += " | "; 118 lblOutput.Text += "" + Request.Url.Query + ""; 119 lblOutput.Text += " |
"; 123 lblOutput.Text += "Request.Url.Scheme:"; 124 lblOutput.Text += " | "; 125 lblOutput.Text += "" + Request.Url.Scheme + ""; 126 lblOutput.Text += " |
"; 130 lblOutput.Text += "Request.Url.Segments:"; 131 lblOutput.Text += " | "; 132 string[] segments = Request.Url.Segments; 133 foreach (string s in segments) 134 lblOutput.Text += "" + s + ""; 135 lblOutput.Text += " |