2012-02-03 69 views
0

一旦我點擊按鈕,我的image1將顯示圖像;Imagebutton不顯示圖像

當我我的ImagePath設置爲

Server.MapPath("~/SME IMAGE/anonymous_avatar.gif"); 

我的網址無法正常工作

但是當我把我的路徑

"~/SME IMAGE/anonymous_avatar.gif" 

我的網址工作

所以基本上什麼發生到我的Server.MapPath?

try 
    { 

     string imagePath = Server.MapPath("~/SME IMAGE/anonymous_avatar.gif"); 
     Image1.ImageUrl = imagePath; 
     Response.Write("success"); 
    } 
    catch (Exception ex) 
    { 
     Response.Write(ex.Message.ToString()); 
    } 
+0

呈現頁面(在瀏覽器中)的兩個URL之間有什麼區別? – Niklas 2012-02-03 07:57:41

+0

@尼克拉斯,我認爲他們沒有什麼不同,是否有任何方法來檢查差異? – 2012-02-03 08:04:28

回答

0

Server.MapPath它將「web」路徑轉換爲本地路徑。
因此Server.MapPath("Server.MapPath")它會給你的文件的本地位置。
要指定這爲ImageUrl屬性,但是這將被轉換爲類似"www.mywebsite.com/C:\wwwroot\somefile.gif"

+1

oic,謝謝爲你的答案=) – 2012-02-03 08:11:07

+0

抱歉跳躍像這樣runat =「服務器」:)我誤讀你的問題 – 2012-02-03 08:12:01

0

儘量不要設置Server.MapPath - 只需使用:

Image1.ImageUrl = "~/SME IMAGE/anonymous_avatar.gif"; 

也許確保網址存在,並且也許添加%20,而不是在路徑的空間。 (也儘量避免路徑中的空格)

+0

感謝您的答案,我只是好奇它不能使用Server.MapPath =( – 2012-02-03 08:07:39

+0

因爲Server.MapPath返回對應於Web服務器上指定的虛擬路徑的物理文件路徑。:-) – janhartmann 2012-02-03 08:09:59

0

Server.MapPath返回對應到Web服務器上的指定虛擬路徑物理文件路徑。所以,當你設置

string imagePath = Server.MapPath("~/SME IMAGE/anonymous_avatar.gif"); 

它可能返回類似C:\Inetpub\wwwroot\website\SME IMAGE\anonymous_avatar.gif這實際上是該文件的物理路徑。

Image.ImageUrl property接受要在Image控件中顯示的圖像的URL。您可以使用相對或絕對網址。 MSDN說:

相對URL將圖像的位置與網頁的位置關聯起來,而不指定服務器上的完整路徑 。 路徑是相對於網頁的位置。這使得更容易 將整個站點移動到服務器上的另一個目錄,而無需 更新代碼。絕對URL提供了完整路徑,所以 將站點移動到另一個目錄需要您更新 代碼。

+0

這是如此清楚!謝謝你的答案=) – 2012-02-03 08:12:35