2012-01-25 66 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)