---------- 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("網址: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(" | "); sb.Append("");
string[] segments = Request.Url.Segments;
foreach (string s in segments)
{
sb.Append("" + s + "");
sb.Append(" "); } sb.Append(" | ");
sb.Append("