太久没用纪录一下
感谢保哥知识分享
举例我们的为网址:http://localhost:1897/News/Press/Content.aspx/123?id=1#toc
Request.ApplicationPath
=> /
Request.PhysicalPath
=> D:\Projects\Solution\web\News\Press\Content.aspx
System.IO.Path.GetDirectoryName(Request.PhysicalPath)
=> D:\Projects\Solution\web\News\Press
Request.PhysicalApplicationPath
=> D:\Projects\Solution\web\
System.IO.Path.GetFileName(Request.PhysicalPath)
=> Content.aspx
Request.CurrentExecutionFilePath
=> /News/Press/Content.aspx
Request.FilePath
=> News/Press/Content.aspx
Request.Path
=> /News/Press/Content.aspx/123
Request.RawUrl
=> /News/Press/Content.aspx/123?id=1
Request.Url.AbsolutePath
=> /News/Press/Content.aspx/123
Request.Url.AbsoluteUri
=> http://localhost:1897/News/Press/Content.aspx/123?id=1
Request.Url.Scheme
=> http
Request.Url.Host
=> localhost
Request.Url.Port
=> 1897
Request.Url.Authority
=> localhost:1897
Request.Url.LocalPath
=> /News/Press/Content.aspx/123
Request.PathInfo
=> /123
Request.Url.PathAndQuery
=> /News/Press/Content.aspx/123?id=1
Request.Url.Query
=> ?id=1
Request.Url.Fragment
=>
Request.Url.Segments
=> /
=> News/
=> Press/
=> Content.aspx/
=> 123
所以当你看了这张表之后,你还会想用 Request.Url.PathAndQuery.Substring(1, Request.Url.PathAndQuery.IndexOf("/", 1)-1) 这种写法吗?
用这样写 Request.Url.Segments[1].Replace("/", "") 不是又短又直觉吗? ^_^
注意事项
当程式部署(Deploy)到 IIS 6 之后,假设你的网址是 http://localhost:1897/News/Press/Content.aspx/123? ,IIS 不知为何会将问号 ( ? ) 给删除掉,以致于 ASP.NET 使用 Request.Url.Query 是空字串!如果你的程式有判断到这部分时就要特别注意,因为照理说应该会抓到才对!不知道是不是 IIS 6 的 Bug
资料来源 来自 The Will Will Web
=>
https://blog.miniasp.com/post/2008/02/10/How-Do-I-Get-Paths-and-URL-fragments-from-the-HttpRequest-object