一 ASP.NET常用路徑(path)擷取方法與格式對照表
假設我們的網址為http://localhost:1897/ News/Press/Content.aspx?id=1019
跟 Browser Request 的網址相關的屬性與方法 |
輸出(output)執行個體 |
備 注 |
Request.ApplicationPath |
/ |
指的是當前的application(應用程式)的目錄 |
Request.PhysicalPath |
D:\Projects\Solution\web\News\Press\Content.aspx |
磁碟機代號:\父目錄\子目錄\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 |
/ News/ Press/ Content.aspx |
|
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 值為 ""(Null 字元串)
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));
}