ASP.NET 進階編程基礎第八篇—Request對象和虛擬路徑

來源:互聯網
上載者:User

前言: 這篇我們開始說一下虛擬路徑和Request對象,這些在asp.net中都是非常常用的小知識點,希望我們通過看這篇部落格能夠更加的加深對這幾個知識點的學習,尤其是虛擬路徑的”~”的使用還有Request對象的一些屬性等。

  1. 特殊路徑標示”~” 虛擬路徑

(1) ”/表示網站根目錄(網域名稱),../表示上級目錄,./表示目前的目錄”等Http標準定位不一樣,~是ASP.NET定義的特殊符號,是ASP.NET內部進行推薦的用法,推薦資源定位都使用~從應用根目錄開始定義,應用根目錄和網站根目錄的區別在於:如果將一個應用程式部署到http://www.cnblogs.com/hanyinglong這個目錄下面,應用程式的根目錄就是Http://www.cnblogs.com/hanyinglong,網站的根目錄是:http://www.cnblogs.com。因此最好用”~”,”~”並不會被瀏覽器認,因此ASP.NET會將這個路徑轉換為相對於網站的根目錄的全路徑在輸出到瀏覽器中。

  1. 編程處理”~”

(1) 如果在服務端控制項中(使用runat=”server”的控制項)會自動將”~”進行轉換,如果在HTML控制項或者需要在代碼中轉換的話可以使用VirtualPathUtility類中靜態方法進行虛擬路徑,全路徑等的轉換,比如:VirtualPathUtility.ToAbsolute(“~/a/b.aspx”)就是將虛擬路徑轉換為相對應於應用程式根目錄的全路徑,也就是”WebSite/a/b.aspx”。

(2) VirtualPathUtility類的主要方法

 1) string AppendTrailingSlash(string VirtualPath);如果路徑VirtualPath最後沒有”/”則添加。

 2) string Combine(string basePath,string relativePath);將兩個路徑進行合并。

 3) string GetDirectory(string virtualPath); 返回虛擬路徑的目錄部分。

 4) string MakeRelative(string fromPath,string toPath); 計算兩個虛擬路徑的相對路徑。

 5) ToAbsolute:轉換為絕對路徑。

(3) 舉例說明:建立一個Web項目,起名為路徑.aspx

   <a href="/a.htm">a</a>&nbsp;

   <a href="~/b.htm">b</a>&nbsp;

   <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="/a.htm">ce1</asp:HyperLink>&nbsp;

   <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/b.htm">ce2</asp:HyperLink>

 然後拖放兩個HyperLink控制項,給兩個控制項的Text起名為Ce1,ce2,給兩個控制項的NavigateURl賦值為”/b.htm”,”~/b.htm”。在Page_Load事件下面寫入如下代碼:

Response.Write("<a href='" + VirtualPathUtility.ToAbsolute("~/a/b.htm") + "'>動態</a>" + "<br />");

Response.Write(VirtualPathUtility.Combine(VirtualPathUtility.AppendTrailingSlash("~/a/b"), "c.htm"));

  1. Request對象

(1) Request.AppRelativeCurrentExecutionFilePath,擷取當前執行請求的相對於應用根目錄的虛擬路徑,以”~”開頭,比如:”~/第七篇/index.aspx”。

Response.Write(Request.AppRelativeCurrentExecutionFilePath);

(2) Request.physicalApplicationPath,擷取當前應用的實體路徑,比如:E:\programmer\黑馬程式員練習題\aspnetpractice\ashx\

Response.Write(Request.PhysicalApplicationPath);

(3) Request.PhysicalPath,擷取當前請求的實體路徑,比如:E:\programmer\黑馬程式員練習題\aspnetpractice\ashx\第七篇\index.aspx

Response.Write(Request.PhysicalPath + "<br />");

(4) Request.RawURL,獲得原始請求URL,比如:/ashx/第7篇/Index.aspx,Request.URL獲得請求的URL,比如:http://localhost:2602/ashx/第7篇/Index.aspx,區別涉及到URL重寫的問題。

      Response.Write(Request.RawUrl + "<br />");

      Response.Write(Request.Url + "<br />");

(5) Request.UrlReferrer網頁的來源,可以根據這個判斷從百度搜到的哪個關鍵字,防下載盜鏈,放圖片盜鏈,可以偽造“本圖片僅供部落格園內部交流使用”,全域防盜鏈用Globals.asax。

(6) Request.UserHostAddress獲得訪問者的IP地址

Response.Write(Request.UserHostAddress + "<br />");

(7) Request.UserLanguage獲得訪問者瀏覽器支援的語言,可以通過這個實現對不同語言的人顯示不同頁面。

Response.Write(Request.UserLanguages + "<br />");

(8) Request.Cookies擷取瀏覽器發過來的瀏覽器端的Cookie,從他裡面讀取Cookie值,比如:context.Request.Cookies[“mysessionID”],使用Request.Cookie的時候一般只是讀取,將Cookie寫回瀏覽器要用Response.Cookies。

(9) Request.MapPath(virtualPath)將虛擬路徑轉換為磁碟上的實體路徑,request.Mappath(~/第七篇/index.aspx)就會得到:E:\programmer\黑馬程式員練習題\aspnetpractice\ashx\第七篇\index.aspx

 Response.Write(Request.MapPath("~/第七篇/index.aspx"));

(10) 建立一個檔案夾Request對象,然後建立一個一般處理常式,建立一個HTML頁面,在HTML頁中寫入如下代碼:

<img src="圖片.ashx" />

然後在.ashx頁面中寫入如下代碼:

 1  context.Response.ContentType = "image/JPEG"; 2  3         string fullPath = HttpContext.Current.Server.MapPath("1.jpg"); 4  5         using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(fullPath)) 6  7         { 8  9             using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))10 11             {12 13                 if (context.Request.UrlReferrer == null) //如果直接瀏覽則沒有URlReferrer14 15                 {16 17                     //g.Clear(System.Drawing.Color.White);18 19                     g.DrawString("禁止直接瀏覽圖片,請在頁面中查看", new System.Drawing.Font("宋體", 20), System.Drawing.Brushes.Red, 0, 0);20 21                 }22 23                 else if (context.Request.UrlReferrer.Host != "localhost")24 25                 {26 27                     g.Clear(System.Drawing.Color.White);28 29                     g.DrawString("本圖片僅限內部使用", new System.Drawing.Font("宋體", 20), System.Drawing.Brushes.Red, 0, 0);30 31                 }32 33                 g.DrawString("你的IP是:" + context.Request.UserHostAddress, new System.Drawing.Font("宋體", 20), System.Drawing.Brushes.Red, 0, 0);34 35                 if (context.Request.UserHostAddress == "127.0.0.1" || context.Request.UserHostAddress == "192.168.0.1")36 37                 {38 39                     //如果直接存取圖片URLReferrer就是null,如果是嵌入到別的網頁中的請求,URLReferrer就是頁面的地址40 41                     g.Clear(System.Drawing.Color.Blue);42 43                     g.DrawString("IP被屏蔽", new System.Drawing.Font("宋體", 20), System.Drawing.Brushes.Red, 0, 0);44 45                 }46 47             }48 49             bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);50 51         }

注釋:Request對象和虛擬路徑我就說到這裡了,下節部落格是Response對象和Server對象。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.