一 ASP.NET經常使用路徑(path)獲取方法與格式對照表html
假設咱們的網址爲http://localhost:1897/ News/Press/Content.aspx?id=1019web
跟 Browser Request 的網址相關的屬性與方法windows |
輸出(output)實例服務器 |
備 注app |
Request.ApplicationPath 工具 |
/ post |
指的是當前的application(應用程序)的目錄ui |
Request.PhysicalPath url |
D:\Projects\Solution\web\News\Press\Content.aspxspa |
磁盤驅動器代號:\父目錄\子目錄\Content.aspx |
Request.PhysicalApplicationPath |
D:\Projects\Solution\web\ |
磁盤驅動器代號:\父目錄\子目錄\ |
Request.CurrentExecutionFilePath |
/News/Press/Content.aspx |
|
Request.FilePath |
/News/Press/Content.aspx |
對應於iis的虛擬目錄。 |
Request.Path |
/News/Press/Content.aspx |
當前請求的虛擬路徑。Path 是 FilePath 和 PathInfo 尾部的串聯。*(見下面詳細講解) |
Server.MapPath(string url) |
例http://www.example.com/1/index.html, 假設你的應用程序在c:/iis/MySite中,那麼就是c:/iis/MySite/1/index.html |
將url映射爲服務器上的物理路徑 |
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 |
/ |
|
Request.Url.Segments[0] |
/ |
|
System.IO.Path.GetDirectoryName(Request.PhysicalPath) |
D:\Projects\Solution\web\News\Press |
磁盤驅動器代號:\父目錄\子目錄\
|
System.IO.Path.GetFileName(Request.PhysicalPath) |
Content.aspx |
|
(接上面*) Request.FilePath, Request.PathInfo, Request.Path, RequestRawUrl
若是請求的地址爲http://www.cnblogs.com/default.aspx/books則
Request.FilePath值爲http://www.cnblogs.com/default.aspx
Request.PathInfo 值爲 /books
Request.Path 值爲 http://www.cnblogs.com/default.aspx/books
Request.RawUrl 值爲 http://www.cnblogs.com/default.aspx/books
若是請求地址爲http://www.cnblogs.com/defaut.aspx?id=1&name=kk則
Request.FilePath值爲http://www.cnblogs.com/default.aspx
Request.PathInfo 值爲 ""(空字符串)
Request.Path 值爲 http://www.cnblogs.com/default.aspx
Request.RawUrl 值爲 http://www.cnblogs.com/default.aspx?id=1&name=kk
二 Request.ServerVariables集合中獲取到的相關信息:
左列是服務器變量名,右側是值,值是經過Request.ServerVariables[服務器變量名]獲取的
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是一個很強大的工具,能夠幫助咱們獲取不少client和web宿主的信息,有興趣的朋友能夠經過如下代碼看看它到底包含什麼信息
foreach (string s in Request.ServerVariables)
{
Response.Write(s + " : " + Request.ServerVariables[s] + "<br /><br />");
}
三 path轉換
1.轉換爲服務器端路徑(Server.MapPath)
web服務器端開發設計一個有趣的問題就是,地址轉換。好比http地址/images/a.txt,若是你想在服務器端經過io讀取這個文件,就得有這個文件的「本機地址(形如c:\windows\system32\xx.dll)」,這時Server.MapPath就頗有用了
Response.Write(Request.MapPath(Request.Path)); 輸出爲 D:\VssWorkFolder\British_School_MIS\src\WebSite\SysOption\BillingSetup1.aspx
2.轉換爲http地址(Page.ResolveClientUrl Page.ResolveUrl)
Response.Write(Page.ResolveClientUrl("~/a/a.jpg")); 輸出爲 ../a/a.jpg
Response.Write(Page.ResolveUrl("~/a/a.jpg")); 輸出爲 /a/a.jpg
另外,咱們使用upload控件上傳文件時,用HttpPostedFile 。例如:
HttpPostedFile file = context.Request.Files[i];//這裏的context.Request.Files就是上傳的文件集合.
PS:此處乃是利用HttpHandler..在Page頁面中能夠本身用其它辦法多文件上傳.
接着如何保存文件呢?
利用HttpPostedFile的SaveAs方法便可,如: file.SaveAs(SpecifiedPath);
此處的SpecifiedPath是上傳文件的絕對路徑.
至於如何獲取上傳文件的路徑.咱們能夠利用Path類.來操做File.HttpPostedFile類中也包含了文件的基本信息.如文件名,大小,路徑等等.Path類操做更齊全而已.接着就能夠利用Server.MapPath()方法來進行轉換.
爲檢驗上面的理論,你能夠編寫一段code跑下就一清二楚啦。例:
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
// 原則上你應該沒法從 Request.Url.Fragment 取得任何數據,由於一般 Browser 不會送出 #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());
參考的文章:
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
若有錯誤,請不吝指出。
另加上一個實例:
// 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)); }