2012-01-25 115 views
1

我已經創建了自定義dnn模塊。 模塊是一個單獨的程序集。 在模塊項目中我有文件夾Images。 我有一個可怕的問題來引用具有相對路徑的圖像。 我試過問題wwith DotNetNuke中的相對路徑

background: url(~/Images/image01.png) 
background: url(~/MyCustomModule/Images/image01.png) 
background: url(~/DesktopModules/MyCustomModule/Images/image01.png) 
etc. 

沒有什麼效果。 才使它的工作方式是,當我寫的東西,如:

background: url(../../DesktopModules/MyCustomModule/Images/image01.png) 

但這個工程我的生產服務器上而不是在我的本地DNN安裝目錄。 是否有任何正確的方法來引用此文件夾中的圖像?

回答

1

您在頂部示例中使用的鏈接通常不會在CSS文件中工作。 ~是由ASP.NET處理的一些特殊內容,表示應用程序的根。你的CSS文件將不知道那是什麼,並將從字面上傳遞它,這可能會破壞鏈接。

不是使用相對路徑,而是使用以斜槓開頭的根路徑?什麼都是正確的文件所在的位置:

background: url(/DesktopModules/MyCustomModule/Images/image01.png) 
background: url(/MyApplication/DesktopModules/MyCustomModule/Images/image01.png) 
background: url(/MyApp/Subdir/DesktopModules/MyCustomModule/Images/image01.png) 

只要確保URL以領先/開始表示

+0

當我輸入'background:url(/ DesktopMo dules/MyCustomModule/Images/image01.png)'它適用於生產,但不適用於本地dnn :( – 1110

0

css文件「網站的根」不知道什麼東西〜字符。路徑應該是相對於那些css文件的位置

8

在您的CSS文件中,您有幾個選項。

使用紮根路徑

background: url(/DesktopModules/MyModule/Images/MyImage.png); 

,但前提是沒有這將工作在一個「子門戶」或非虛擬目錄設置。

最好的選擇,如果你能做到這一點是讓URL的相對的CSS文件本身...

所以,如果你的CSS是在/ DesktopModules/MyModule的你會那麼用戶

background: url(Images/MyImage.png); 

這應該適用於這兩種情況。

4

原因:服務器名稱丟失。在本地機器上,地址看起來像http://localhost/<servername> - 比如我的是http://localhost/dnn6.

它不會在本地計算機上工作的原因是因爲瀏覽器是在http://localhost/Images/image01.png or http://localhost/DesktopModules/MyCustomModule/Images/image01.png.

尋找圖片,我更喜歡插我的CSS和其他腳本背後的代碼。這樣服務器名稱就包含在內,它可以在生產和開發服務器上運行。

LiteralControl litScripts = new LiteralControl(); 
litScripts.Text +="<link href=\"" + this.TemplateSourceDirectory + 
"/CSS/Form.css\" rel=\"stylesheet\" type=\"text/css\" />"; 
Page.Header.Controls.Add(litScripts); 

在我的生產服務器,該URL看起來像

<link href="/DesktopModules/MyCustomModule/CSS/Form.css" 
rel="stylesheet" type="text/css" /> 

和開發服務器上,它被插入的

<link href="/DNN6/DesktopModules/MyCustomModule/CSS/Form.css" rel="stylesheet" type="text/css" /> 

現在編輯Form.css看起來像這樣,

background: url(../Images/image01.png)