ASP.NET內抓取網址的方式一覽表(Request)
- 2008-04-23
- 79409
- 0
有些時候我們需要抓到網址中的某一字串來作判斷,你還在用老舊的IndexOf 與 Substring 慢慢組嗎?別鬧了.NET有提供更聰明的作法,以下就列出一張表,各位就很清楚啦。
假設我們的網址為http://demo.tc/Default3.aspx?id=1019&uu=77
Request.ApplicationPath //output:/ Request.PhysicalPath //output:磁碟機代號:\父目錄\子目錄\Default3.aspx System.IO.Path.GetDirectoryName(Request.PhysicalPath) //output:磁碟機代號:\父目錄\子目錄 Request.PhysicalApplicationPath //output:磁碟機代號:\父目錄\子目錄\ System.IO.Path.GetFileName(Request.PhysicalPath) //output:Default3.aspx Request.CurrentExecutionFilePath //output:/Default3.aspx Request.FilePath //output:/Default3.aspx Request.Path //output:/Default3.aspx Request.RawUrl //output:/Default3.aspx?id=1019&uu=77 Request.Url.AbsolutePath //output:/Default3.aspx Request.Url.AbsoluteUri //output:http://demo.tc/Default3.aspx?id=1019(這是上一頁的網址) Request.Url.Scheme //output:http Request.Url.Host //output:demo.tc Request.Url.Port //output:80 Request.Url.Authority //output:demo.tc Request.Url.LocalPath //output:/Default3.aspx Request.PathInfo //output: Request.Url.PathAndQuery //output:/Default3.aspx?id=1019&uu=77 Request.Url.Query //output:?id=1019&uu=77 Request.Url.Fragment //output: Request.Url.Segments[0] //output:/ Request.Url.Segments[1] //output:Default3.aspx
▲但是因為小鋪的網址太短了,所以看不太出端倪,不如您就把下方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
回應討論