---------- 20190624 ref: https://blog.miniasp.com/post/2008/02/10/How-Do-I-Get-Paths-and-URL-fragments-from-the-HttpRequest-object.aspx https://adon988.logdown.com/posts/7624741-aspnet-c-url-parameter-parsing-get-url-parameters 網址例: http://localhost:1897/News/Press/Content.aspx/123?id=1#toc https://my.url.com:8080/Detail/Page/List.aspx/showmore?mid=20#main /* ----------------------------------------------------- ---------------------------------------------------- Request.ApplicationPath / / Request.CurrentExecutionFilePath /News/Press/Content.aspx /Detail/Page/List.aspx Request.FilePath /News/Press/Content.aspx /Detail/Page/List.aspx ----------------------------------------------------- ---------------------------------------------------- Request.Path /News/Press/Content.aspx/123 /Detail/Page/List.aspx/showmore Request.PathInfo /123 /showmore Request.PhysicalApplicationPath D:\Projects\Solution\web\ C:\wwwroot\ Request.PhysicalPath D:\Projects\Solution\web\News\Press\Content.aspx C:\wwwroot\Detail\Page\List.aspx Request.RawUrl /News/Press/Content.aspx/123?id=1 /Detail/Page/List.aspx/showmore?mid=20 ----------------------------------------------------- ---------------------------------------------------- Request.Url.AbsolutePath /News/Press/Content.aspx/123 /Detail/Page/List.aspx/showmore Request.Url.AbsoluteUri http://localhost:1897/News/Press/Content.aspx/123?id=1 https://my.url.com:8080/Detail/Page/List.aspx/showmore?mid=20 Request.Url.Authority localhost:1897 my.url.com:8080 ----------------------------------------------------- ---------------------------------------------------- Request.Url.Fragment Request.Url.Scheme http https Request.Url.Host localhost my.url.com Request.Url.Port 1897 8080 Request.Url.LocalPath /News/Press/Content.aspx/123 /Detail/Page/List.aspx/showmore ----------------------------------------------------- ---------------------------------------------------- Request.Url.PathAndQuery /News/Press/Content.aspx/123?id=1 /Detail/Page/List.aspx/showmore?mid=20 Request.Url.Query ?id=1 ?mid=20 Request.Url.Segments /, News/, Press/, Content.aspx/, 123 /, Detail/, Page/, List.aspx/, showmore ----------------------------------------------------- ---------------------------------------------------- */ 所以當你看了這張表之後,你還會想用 Request.Url.PathAndQuery.Substring(1, Request.Url.PathAndQuery.IndexOf("/", 1)-1) 這種寫法嗎? 用這樣寫 Request.Url.Segments[1].Replace("/", "") 不是又短又直覺嗎? ^_^ 以下是產生以上表格的程式碼: protected void Page_Load(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(); sb.Append(""); sb.Append(""); // Request.ApplicationPath sb.Append(""); // Request.PhysicalPath sb.Append(""); // System.IO.Path.GetDirectoryName(Request.PhysicalPath) sb.Append(""); // Request.PhysicalApplicationPath sb.Append(""); // System.IO.Path.GetFileName(Request.PhysicalPath) sb.Append(""); // Request.CurrentExecutionFilePath sb.Append(""); // Request.FilePath sb.Append(""); // Request.Path sb.Append(""); // Request.RawUrl sb.Append(""); // Request.Url.AbsolutePath sb.Append(""); // Request.Url.AbsoluteUri sb.Append(""); // Request.Url.Scheme sb.Append(""); // Request.Url.Host sb.Append(""); // Request.Url.Port sb.Append(""); // Request.Url.Authority sb.Append(""); // local Request.Url.LocalPath sb.Append(""); // Request.PathInfo sb.Append(""); // Request.Url.PathAndQuery sb.Append(""); // Request.Url.Query sb.Append(""); // Request.Url.Fragment // 原則上你應該無法從 Request.Url.Fragment 取得任何資料,因為通常 Browser 不會送出 #toc 這個部分 sb.Append(""); // Request.Url.Segments sb.Append(""); sb.Append(""); sb.Append(""); sb.Append(""); sb.Append("
"); sb.Append("網址:http://localhost:1897/News/Press/Content.aspx/123?id=1#toc"); sb.Append("
"); sb.Append("Request.ApplicationPath"); sb.Append(""); sb.Append("" + Request.ApplicationPath + ""); sb.Append("
"); sb.Append("Request.PhysicalPath"); sb.Append(""); sb.Append("" + Request.PhysicalPath + ""); sb.Append("
"); sb.Append("System.IO.Path.GetDirectoryName(Request.PhysicalPath)"); sb.Append(""); sb.Append("" + System.IO.Path.GetDirectoryName(Request.PhysicalPath) + ""); sb.Append("
"); sb.Append("Request.PhysicalApplicationPath"); sb.Append(""); sb.Append("" + Request.PhysicalApplicationPath + ""); sb.Append("
"); sb.Append("System.IO.Path.GetFileName(Request.PhysicalPath)"); sb.Append(""); sb.Append("" + System.IO.Path.GetFileName(Request.PhysicalPath) + ""); sb.Append("
"); sb.Append("Request.CurrentExecutionFilePath"); sb.Append(""); sb.Append("" + Request.CurrentExecutionFilePath + ""); sb.Append("
"); sb.Append("Request.FilePath"); sb.Append(""); sb.Append("" + Request.FilePath + ""); sb.Append("
"); sb.Append("Request.Path"); sb.Append(""); sb.Append("" + Request.Path + ""); sb.Append("
"); sb.Append("Request.RawUrl"); sb.Append(""); sb.Append("" + Request.RawUrl + ""); sb.Append("
"); sb.Append("Request.Url.AbsolutePath"); sb.Append(""); sb.Append("" + Request.Url.AbsolutePath + ""); sb.Append("
"); sb.Append("Request.Url.AbsoluteUri"); sb.Append(""); sb.Append("" + Request.Url.AbsoluteUri + ""); sb.Append("
"); sb.Append("Request.Url.Scheme"); sb.Append(""); sb.Append("" + Request.Url.Scheme + ""); sb.Append("
"); sb.Append("Request.Url.Host"); sb.Append(""); sb.Append("" + Request.Url.Host + ""); sb.Append("
"); sb.Append("Request.Url.Port"); sb.Append(""); sb.Append("" + Request.Url.Port + ""); sb.Append("
"); sb.Append("Request.Url.Authority"); sb.Append(""); sb.Append("" + Request.Url.Authority + ""); sb.Append("
"); sb.Append("Request.Url.LocalPath"); sb.Append(""); sb.Append("" + Request.Url.LocalPath + ""); sb.Append("
"); sb.Append("Request.PathInfo"); sb.Append(""); sb.Append("" + Request.PathInfo + ""); sb.Append("
"); sb.Append("Request.Url.PathAndQuery"); sb.Append(""); sb.Append("" + Request.Url.PathAndQuery + ""); sb.Append("
"); sb.Append("Request.Url.Query"); sb.Append(""); sb.Append("" + Request.Url.Query + ""); sb.Append("
"); sb.Append("Request.Url.Fragment"); sb.Append(""); sb.Append("" + Request.Url.Fragment + ""); sb.Append("
"); sb.Append("Request.Url.Segments"); sb.Append(""); string[] segments = Request.Url.Segments; foreach (string s in segments) { sb.Append("" + s + ""); sb.Append("
"); } sb.Append("
"); ltlTable.Text = sb.ToString(); } 注意事項 當程式部署(Deploy)到 IIS 6 之後,假設你的網址是 http://localhost:1897/News/Press/Content.aspx/123? ,IIS 不知為何會將問號 ( ? ) 給刪除掉,以致於 ASP.NET 使用 Request.Url.Query 是空字串!如果你的程式有判斷到這部分時就要特別注意,因為照理說應該會抓到才對!不知道是不是 IIS 6 的 Bug ?