ASP. NETPath)Comparison table of obtaining methods and formats
Suppose our website is http: // localhost: 1897/news/press/content. aspx? Id = 1019
AndBrowser requestURL-related attributes and Methods |
Output) Instance |
BackupNote |
Request. applicationpath |
/ |
Indicates the current applicationProgram) Directory |
Request. physicalpath |
D: \ projects \ solution \ WEB \ news \ press \ content. aspx |
Disk Drive code: \ parent directory \ subdirectory \ content. aspx |
Request. physicalapplicationpath |
D: \ projects \ solution \ WEB \ |
Disk Drive code: \ parent directory \ subdirectory \ |
Request. currentexecutionfilepath |
/News/press/content. aspx |
|
Request. filepath |
/News/press/content. aspx |
Corresponds to the virtual directory of IIS. |
Request. Path |
/News/press/content. aspx |
The Virtual Path of the current request. PATH is the concatenation of filepath and pathinfo. * (See the following for details) |
Server. mappath (string URL) |
For example, if your application is in C:/IIS/mysite, then C:/IIS/mysite/1/index.html |
Map a URL to a physical path on the server
|
Request. rawurl |
/News/press/content. aspx? Id = 1019 |
|
Request. url. absolutepath |
/News/press/content. aspx |
|
Request. url. absoluteuri |
Http: // localhost: 1897/content. aspx? Id = 1019 |
|
Request. url. localpath |
/News/press // content. aspx |
|
Request. url. pathandquery |
/News/press // content. aspx? Id = 1019 & UU = 77 |
|
Request. url. Scheme |
HTTP |
|
Request. url. Host |
Localhost |
|
Request. url. Port |
1987 |
|
Request. url. Authority |
Localhost: 1897 |
|
Request. url. Query |
? Id = 1019 |
|
Request. url. Query [ID] |
1019 |
|
Request. url. Fragments |
/ News/ Press/ Content. aspx |
|
Request. url. segments [0] |
/ |
|
System. Io. Path. getdirectoryname (request. physicalpath) |
D: \ projects \ solution \ WEB \ news \ Press |
Disk Drive code :\Parent directory \Subdirectory \ |
System. Io. Path. getfilename (request. physicalpath) |
Content. aspx |
|
(Link *) request. filepath, request. pathinfo, request. Path, requestrawurl
If the request address is http://www.cnblogs.com/default.aspx/booksthen
Request. filepath value: http://www.cnblogs.com/default.aspx
The request. pathinfo value is/books.
The request. PATH value is http://www.cnblogs.com/default.aspx/books
Request. rawurl value is http://www.cnblogs.com/default.aspx/books
If the request address is http://www.cnblogs.com/defaut.aspx? Id = 1 & name = kk
Request. filepath value: http://www.cnblogs.com/default.aspx
Request. pathinfo value: "" (Null String)
The request. PATH value is http://www.cnblogs.com/default.aspx
The request. rawurl value is http://www.cnblogs.com/default.aspx? Id = 1 & name = kk
2 request. servervariablesInformation obtained from the collection:
The left column is the server variable name, the right column is the value, and the value is obtained through request. servervariables [server variable name ].
Appl_md_path:/lm/w3svc/894523/root
Appl_physical_path: D: \ vssworkfolder \ briish_school_mis \ SRC \ website \
Instance_meta_path:/lm/w3svc/894523
Local_addr: 192.168.1.6
Path_info:/sysoption/billingsetup1.aspx
Path_translated: D: \ vssworkfolder \ briish_school_mis \ SRC \ website \ sysoption \ billingsetup1.aspx
Remote_addr: 192.168.1.6
Remote_host: 192.168.1.6
Script_name:/sysoption/billingsetup1.aspx
SERVER_NAME: 192.168.1.6
URL:/sysoption/billingsetup1.aspx
Request. servervariables is a powerful tool that can help us get a lot of client and web host information. If you are interested, you can use the followingCodeSee what information it actually contains.
Foreach (string s in request. servervariables)
{
Response. Write (S + ":" + request. servervariables [s] + "<br/> ");
}
3.PathConversion
1. convert to the server path (server. mappath)
An interesting question about Web server development and design is address translation. For example, HTTP address/images/a.txt. If you want to read the file through Io on the server side, you must have the local address of the file (such as C: \ windows \ system32 \ xx. DLL) ", then server. mappath is useful.
Response. Write (request. mappath (request. Path); output: D: \ vssworkfolder \ briish_school_mis \ SRC \ website \ sysoption \ billingsetup1.aspx
2. Convert to HTTP address (page. resolveclienturl page. resolveurl)
Response. Write (page. resolveclienturl ("~ /A/a.jpg "). The output is ../A/a.jpg.
Response. Write (page. resolveurl ("~ /A/a.jpg "). The output is/A/a.jpg.
In addition, when we use the upload control to upload a file, we use httppostedfile. For example:
Httppostedfile file = context. Request. Files [I]; // The context. Request. files here is a collection of uploaded files.
PS: httphandler is used here. You can use other methods to upload multiple files on the page.
How can I save the file?
Use the saveas method of httppostedfile, for example, file. saveas (specifiedpath );
The specifiedpath here is the absolute path of the uploaded file.
How to obtain the path of the uploaded file. we can use the path class. to operate the file. the httppostedfile class also contains the basic information of the file. such as file name, size, and path. path class operations are more complete. then you can use server. mappath () method for conversion.
To test the above theory, you can write a piece of code to clearly understand it. Example:
Stringbuilder Req = new stringbuilder ();
Req. append ("<Table cellpadding = 3 cellspacing = 0 border = 1> ");
// Request. applicationpath
Req. append ("<tr> <TD> ");
Req. append ("request. applicationpath ");
Req. append ("</TD> <TD> ");
Req. append ("<B>" + request. applicationpath + "</B> ");
Req. append ("</TD> </tr> ");
// Request. physicalpath
Req. append ("<tr> <TD> ");
Req. append ("request. physicalpath ");
Req. append ("</TD> <TD> ");
Req. append ("<B>" + request. physicalpath + "</B> ");
Req. append ("</TD> </tr> ");
// System. Io. Path. getdirectoryname (request. physicalpath)
Req. append ("<tr> <TD> ");
Req. append ("system. Io. Path. getdirectoryname (request. physicalpath )");
Req. append ("</TD> <TD> ");
Req. append ("<B>" + system. Io. Path. getdirectoryname (request. physicalpath) + "</B> ");
Req. append ("</TD> </tr> ");
// Request. physicalapplicationpath
Req. append ("<tr> <TD> ");
Req. append ("request. physicalapplicationpath ");
Req. append ("</TD> <TD> ");
Req. append ("<B>" + request. physicalapplicationpath + "</B> ");
Req. append ("</TD> </tr> ");
// System. Io. Path. getfilename (request. physicalpath)
Req. append ("<tr> <TD> ");
Req. append ("system. Io. Path. getfilename (request. physicalpath )");
Req. append ("</TD> <TD> ");
Req. append ("<B>" + system. Io. Path. getfilename (request. physicalpath) + "</B> ");
Req. append ("</TD> </tr> ");
// Request. currentexecutionfilepath
Req. append ("<tr> <TD> ");
Req. append ("request. currentexecutionfilepath ");
Req. append ("</TD> <TD> ");
Req. append ("<B>" + request. currentexecutionfilepath + "</B> ");
Req. append ("</TD> </tr> ");
// Request. filepath
Req. append ("<tr> <TD> ");
Req. append ("request. filepath ");
Req. append ("</TD> <TD> ");
Req. append ("<B>" + request. filepath + "</B> ");
Req. append ("</TD> </tr> ");
// Request. Path
Req. append ("<tr> <TD> ");
Req. append ("request. Path ");
Req. append ("</TD> <TD> ");
Req. append ("<B>" + request. Path + "</B> ");
Req. append ("</TD> </tr> ");
// Request. rawurl
Req. append ("<tr> <TD> ");
Req. append ("request. rawurl ");
Req. append ("</TD> <TD> ");
Req. append ("<B>" + request. rawurl + "</B> ");
Req. append ("</TD> </tr> ");
// Request. url. absolutepath
Req. append ("<tr> <TD> ");
Req. append ("request. url. absolutepath ");
Req. append ("</TD> <TD> ");
Req. append ("<B>" + request. url. absolutepath + "</B> ");
Req. append ("</TD> </tr> ");
// Request. url. absoluteuri
Req. append ("<tr> <TD> ");
Req. append ("request. url. absoluteuri ");
Req. append ("</TD> <TD> ");
Req. append ("<B>" + request. url. absoluteuri + "</B> ");
Req. append ("</TD> </tr> ");
// Request. url. Scheme
Req. append ("<tr> <TD> ");
Req. append ("request. url. Scheme ");
Req. append ("</TD> <TD> ");
Req. append ("<B>" + request. url. scheme + "</B> ");
Req. append ("</TD> </tr> ");
// Request. url. Host
Req. append ("<tr> <TD> ");
Req. append ("request. url. Host ");
Req. append ("</TD> <TD> ");
Req. append ("<B>" + request. url. Host + "</B> ");
Req. append ("</TD> </tr> ");
// Request. url. Port
Req. append ("<tr> <TD> ");
Req. append ("request. url. Port ");
Req. append ("</TD> <TD> ");
Req. append ("<B>" + request. url. Port + "</B> ");
Req. append ("</TD> </tr> ");
// Request. url. Authority
Req. append ("<tr> <TD> ");
Req. append ("request. url. Authority ");
Req. append ("</TD> <TD> ");
Req. append ("<B>" + request. url. Authority + "</B> ");
Req. append ("</TD> </tr> ");
// Local request. url. localpath
Req. append ("<tr> <TD> ");
Req. append ("request. url. localpath ");
Req. append ("</TD> <TD> ");
Req. append ("<B>" + request. url. localpath + "</B> ");
Req. append ("</TD> </tr> ");
// Request. pathinfo
Req. append ("<tr> <TD> ");
Req. append ("request. pathinfo ");
Req. append ("</TD> <TD> ");
Req. append ("<B>" + request. pathinfo + "</B> ");
Req. append ("</TD> </tr> ");
// Request. url. pathandquery
Req. append ("<tr> <TD> ");
Req. append ("request. url. pathandquery ");
Req. append ("</TD> <TD> ");
Req. append ("<B>" + request. url. pathandquery + "</B> ");
Req. append ("</TD> </tr> ");
// Request. url. Query
Req. append ("<tr> <TD> ");
Req. append ("request. url. Query ");
Req. append ("</TD> <TD> ");
Req. append ("<B>" + request. url. query + "</B> ");
Req. 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.
Req. append ("<tr> <TD> ");
Req. append ("request. url. Fragment ");
Req. append ("</TD> <TD> ");
Req. append ("<B>" + request. url. Fragment + "</B> ");
Req. append ("</TD> </tr> ");
// Request. url. segments
Req. append ("<tr> ");
Req. append ("<TD> ");
Req. append ("request. url. segments ");
Req. append ("</TD> ");
Req. append ("<TD> ");
String [] segments = request. url. segments;
Foreach (string s in segments)
{
Req. append ("<B>" + S + "</B> ");
Req. append ("<p> ");
}
Req. append ("</TD> ");
Req. append ("</tr> ");
Req. append ("</table> ");
Response. Write (req. tostring ());
ReferenceArticle:
Http://blog.miniasp.com/post/2008/02/10/How-Do-I-Get-Paths-and-URL-fragments-from-the-HttpRequest-object.aspx
Http://www.cnblogs.com/zyip/archive/2009/08/13/1544968.html
If any error occurs, please do not renew it.
Add another instance:
// builds an absolute URL
Private Static string buildabsolute (string relativeuri)
{< br> // get current URI
URI uri = httpcontext. current. request. URL;
// build absolute path
string APP = httpcontext. current. request. applicationpath;
If (! App. endswith ("/") app + = "/";
relativeuri = relativeuri. trimstart ('/');
// return the absolute path
return httputility. urlpathencode (
string. format ("http: // {0 }:{ 1} {2} {3}",
Uri. host, Uri. port, app, relativeuri);
}