We are developing Web applications that often need to parse every fragment of the Web address (Request.url) to do some judgment. For example, "Http://localhost:1897/News/Press/Content.aspx/123?id=1#toc", we want to get the first name of the website (News) to judge the different page title.
I think a lot of people use string IndexOf methods and Substring methods:
Request.Url.PathAndQuery.Substring (1, Request.Url.PathAndQuery.IndexOf ("/", 1)-1)
This is actually too buried without the strong design of. NET, in fact, the request object has been provided with a lot of convenient property to obtain a fragment of the Web site.
The table below is the Browser and usage of the web address of the Request: Web: 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
So when you look at this watch, do you still want to use Request.Url.PathAndQuery.Substring (1, Request.Url.PathAndQuery.IndexOf ("/", 1)-1) to write this method?
Write request.url.segments[1 in this way]. Is Replace ("/", "" ") short and straight? A variety of ways to get URL information ^_^
============================================================================
Request
In ASP. I often need to use request to get the information about the URL, there are many ways to get the URL information, but I often forget the specific role of various methods, today I wrote a test program, the various methods to get the results listed, and then use a direct reference on the line. The URL address for the
test is http://www.test.com/testweb/default.aspx and the result is as follows:
Request.applicationpath:/testweb
Request.currentexecutionfilepath:/testweb/default.aspx
Request.filepath:/testweb/default.aspx
Request.path:/testweb/default.aspx
Request.pathinfo:
Request.physicalapplicationpath:e:\www\testweb\
Request.physicalpath:e:\www\testweb\default.aspx
Request.rawurl:/testweb/default.aspx
Request.Url.AbsolutePath:/testweb/default.aspx
Request.url.absoluteuri:http://www.test.com/testweb/default.aspx
Request.Url.Host:www.test.com
Request.Url.LocalPath:/testweb/default.aspx
This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/ChaoYang0502/archive/2008/09/20/2956244.aspx
C # get page URL information