an ASP . Common paths (path) get a comparison of methods and formats
Let's say our URL is http://localhost:1897/News/Press/Content.aspx?id=1019.
with Browser Request URL-related properties and methods |
outputs ( output ) Instance |
prepared Note |
Request.applicationpath |
/ |
Refers to the directory of the current application (application) |
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 |
The virtual directory that corresponds to IIS. |
Request.path |
/news/press/content.aspx |
The virtual path of the current request. Path is the concatenation of the FilePath and PathInfo tails. * (see below for detailed explanation) |
Server.MapPath (string URL) |
Example http://www.example.com/1/index.html, assuming your application is in C:/iis/mysite, then it is c:/iis/mysite/1/index.html |
Map URLs to physical paths 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 \ sub-directory \
|
System.IO.Path.GetFileName (Request.PhysicalPath) |
Content.aspx |
|
( next to *) Request.filepath, Request.pathinfo, Request.path, Requestrawurl
If the requested address is Http://www.cnblogs.com/default.aspx/books
Request.filepath value is http://www.cnblogs.com/default.aspx
Request.pathinfo value is/books
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 is http://www.cnblogs.com/default.aspx
Request.pathinfo value is "" (empty string)
Request.path value is http://www.cnblogs.com/default.aspx
Request.rawurl value is Http://www.cnblogs.com/default.aspx?id=1&name=kk
two Request.ServerVariables gets information about the collection:
,
The left column is the server variable name, the right side is the value, the value is obtained through the request.servervariables[server variable name]
Appl_md_path:/lm/w3svc/ 894523/root
appl_physical_path:d:\vssworkfolder\british_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\british_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 hosting information, and interested friends can use the following code to see what information it contains
foreach (string s in Request.ServerVariables)
{
Response.Write (S + ":" + request.servervariables[s] + "<br/><br/>");
}
three Path Conversion
1. Convert to server-side path (Server.MapPath)
Web server-side development and design an interesting problem is that address translation. For example, HTTP address/images/a.txt, if you want to read this file on the server side through IO, you have to have this file "native address (shaped like C:\windows\system32\xx.dll)", then Server.MapPath is very useful
Response.Write (Request.mappath (Request.path)); Output is D:\VssWorkFolder\British_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")); 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 here. Request.Files is the collection of uploaded files.
PS: Here is the use of HttpHandler. You can upload multiple files on the page by yourself in other ways.
Then how to save the file?
You can use Httppostedfile's SaveAs method, such as: file. SaveAs (Specifiedpath);
The specifiedpath here is the absolute path to the uploaded file.
As to how to get the path of the uploaded file. We can use the path class. To manipulate the File.httppostedfile class also contains basic information about the file. such as file name, size, path, and so on. The path class is more complete. You can then use the Server.MapPath () method to convert.
In order to test the above theory, you can write a code run down to be clear. Cases:
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 get any data from Request.Url.Fragment, because usually Browser does not send out #toc this part
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 ());
Reference articles:
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 there is any mistake, please do not hesitate to point out.
Add another example:
Builds an absolute URL
private static string Buildabsolute (String relativeuri)
{
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));
}
Asp. Path in the net