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 each of the Web addresses that are associated with the Request:
| Web address: 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 ("/", "") not short and straight? ^_^
Below is the code for producing the above form:
protected void Page_Load (object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder ();
Sb. Append ("<table cellpadding=3 cellspacing=0 border=1>");
Sb. Append ("<tr><td colspan=2>");
Sb. Append ("website: Http://localhost:1897/News/Press/Content.aspx/123?id=1#toc" >http://localhost:1897/news/ PRESS/CONTENT.ASPX/123?ID=1#TOC sb. Append ("</td></tr>"); ");
//Request.applicationpath
Sb. Append ("<tr><td>");
Sb. Append ("Request.applicationpath");
Sb. Append ("</td><td>");
Sb. Append ("<b>" + Request.applicationpath + "</b>");
Sb. Append ("</td></tr>");
//Request.PhysicalPath
sb. Append ("<tr><td>");
sb. Append ("Request.PhysicalPath");
sb. Append ("</td><td>");
sb. Append ("<b>" + Request.PhysicalPath + "</b>");
sb. Append ("</td></tr>");
//System.IO.Path.GetDirectoryName ( Request.PhysicalPath)
sb. Append ("<tr><td>");
sb. Append ("System.IO.Path.GetDirectoryName (Request.PhysicalPath)");
sb. Append ("</td><td>");
sb. Append ("<b>" + System.IO.Path.GetDirectoryName (Request.PhysicalPath) + "</b>");
sb. Append ("</td></tr>");
//request.physicalapplicationpath
sb. Append ("<tr><td>");
sb. Append ("Request.physicalapplicationpath");
sb. Append ("</td><td>");
sb. Append ("<b>" + Request.physicalapplicationpath + "</b>");
sb. Append ("</td></tr>");
//System.IO.Path.GetFileName ( Request.PhysicalPath)
sb. Append ("<tr><td>");
sb. Append ("System.IO.Path.GetFileName (Request.PhysicalPath)");
sb. Append ("</td><td>");
sb. Append ("<b>" + System.IO.Path.GetFileName (Request.PhysicalPath) + "</b>");
sb. Append ("</td></tr>");
//request.currentexecutionfilepath
sb. Append ("<tr><td>");
sb. Append ("Request.currentexecutionfilepath");
sb. Append ("</td><td>");
sb. Append ("<b>" + Request.currentexecutionfilepath + "</b>");
sb. Append ("</td></tr>");
//request.filepath
sb. Append ("<tr><td>");
sb. Append ("Request.filepath");
sb. Append ("</td><td>");
sb. Append ("<b>" + Request.filepath + "</b>");
sb. Append ("</td></tr>");
//request.path
sb. Append ("<tr><td>");
sb. Append ("Request.path");
sb. Append ("</td><td>");
sb. Append ("<b>" + Request.path + "</b>");
sb. Append ("</td></tr>");
//request.rawurl
sb. Append ("<tr><td>");
sb. Append ("Request.rawurl");
sb. Append ("</td><td>");
sb. Append ("<b>" + Request.rawurl + "</b>");
sb. Append ("</td></tr>");
//Request.Url.AbsolutePath
sb. Append ("<tr><td>");
sb. Append ("Request.Url.AbsolutePath");
sb. Append ("</td><td>");
sb. Append ("<b>" + Request.Url.AbsolutePath + "</b>");
sb. Append ("</td></tr>");
//Request.Url.AbsoluteUri
sb. Append ("<tr><td>");
sb. Append ("Request.Url.AbsoluteUri");
sb. Append ("</td><td>");
sb. Append ("<b>" + Request.Url.AbsoluteUri + "</b>");
sb. Append ("</td></tr>");
//Request.Url.Scheme
sb. Append ("<tr><td>");
sb. Append ("Request.Url.Scheme");
sb. Append ("</td><td>");
sb. Append ("<b>" + Request.Url.Scheme + "</b>");
sb. Append ("</td></tr>");
//Request.Url.Host
sb. Append ("<tr><td>");
sb. Append ("Request.Url.Host");
sb. Append ("</td><td>");
sb. Append ("<b>" + Request.Url.Host + "</b>");
sb. Append ("</td></tr>");
//Request.Url.Port
sb. Append ("<tr><td>");
sb. Append ("Request.Url.Port");
sb. Append ("</td><td>");
sb. Append ("<b>" + Request.Url.Port + "</b>");
sb. Append ("</td></tr>");
//Request.Url.Authority
sb. Append ("<tr><td>");
sb. Append ("Request.Url.Authority");
sb. Append ("</td><td>");
sb. Append ("<b>" + Request.Url.Authority + "</b>");
sb. Append ("</td></tr>");
//local Request.Url.LocalPath
sb. Append ("<tr><td>");
sb. Append ("Request.Url.LocalPath");
sb. Append ("</td><td>");
sb. Append ("<b>" + Request.Url.LocalPath + "</b>");
sb. Append ("</td></tr>");
//request.pathinfo
sb. Append ("<tr><td>");
sb. Append ("Request.pathinfo");
sb. Append ("</td><td>");
sb. Append ("<b>" + request.pathinfo + "</b>");
sb. Append ("</td></tr>");
//Request.Url.PathAndQuery
sb. Append ("<tr><td>");
sb. Append ("Request.Url.PathAndQuery");
sb. Append ("</td><td>");
sb. Append ("<b>" + Request.Url.PathAndQuery + "</b>");
sb. Append ("</td></tr>");
//Request.Url.Query
sb. Append ("<tr><td>");
sb. Append ("Request.Url.Query");
sb. Append ("</td><td>");
sb. Append ("<b>" + Request.Url.Query + "</b>");
sb. Append ("</td></tr>");
//Request.Url.Fragment
//On the principle you should not get any information from Request.Url.Fragment, because usually Browser does not send out #toc this part
Sb. Append ("<tr><td>");
Sb. Append ("Request.Url.Fragment");
Sb. Append ("</td><td>");
Sb. Append ("<b>" + Request.Url.Fragment + "</b>");
Sb. Append ("</td></tr>");
Request.Url.Segments
Sb. Append ("<tr>");
Sb. Append ("<td>");
Sb. Append ("Request.Url.Segments");
Sb. Append ("</td>");
Sb. Append ("<td>");
string[] segments = Request.Url.Segments;
foreach (string s in segments)
{
Sb. Append ("<b>" + S + "</b>");
Sb. Append ("<br/>");
}
Sb. Append ("</td>");
Sb. Append ("</tr>");
Sb. Append ("</table>");
Ltltable.text = sb. ToString ();
}
Request.url usage