When developing Web applications, we often need to parse the URL (request. URL), for example, "http: // localhost: 1897/news/press/content. aspx/123? Id = 1 # TOC ", we want to obtain the name of the first directory (News) in the website to determine different page titles )。
I think many people use the string indexof method and substring method:
Request. url. pathandquery. substring (1, request. url. pathandquery. indexof ("/", 1)-1)
This is too powerful. net design. In fact, the request object has provided many convenient properties for obtaining URL fragments 。
The following table lists the attributes and usage related to browser request URLs:
URL: 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. aspixels/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 after you read this table, you still want to use request. URL. pathandquery. substring (1, request. URL. pathandquery. indexof ("/", 1)-1?
Write Request. url. segments [1]. Replace ("/", "") in this way. Isn't it short and intuitive? Pai_^
The program code that generates the preceding table is as follows:
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 ("url:" 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
// In principle, you should not be able to obtain any data from request. url. fragment, because the browser usually does not send # TOC.
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 ();
}