2011-03-14 54 views
0

我们有一个多语言网站,或文化敏感的,一些静态内容现在需要针对性;为此,我使用主题,因为它似乎达到我想要的最简单的方法,但我不能为我的生活让图像拾起。ASP.NET主题以编程方式:图片路径

我在代码隐藏中设置主题,起初认为可能是这个问题,但在检查时,它看起来像我正在做正确的事情(设置在Pre-Init)。

我希望能够引用使用相对路径,其中App_Themes/ThemeName/会自动解决,如图像,:

<asp:Image runat="server" ImageUrl="images\image.jpg"/> 

无论出于何种原因,但是,图像不被通过拉可言。

这是我们已有的代码来设置主题(唯一真正相关的部分,我敢肯定,作为Theme = CurrentSite.CultureName,这是成功应用):

Private Sub SetTheme() 
    Dim themesPath = Server.MapPath("~/App_Themes") 
    If Directory.Exists(themesPath) Then 
     Dim themePaths = Directory.GetDirectories(themesPath) 
     Dim themePathInfo As DirectoryInfo 
     For Each _path In themePaths 
      themePathInfo = New DirectoryInfo(_path) 
      If Not themePathInfo Is Nothing Then 
       If themePathInfo.Name = CurrentSite.CultureName Then 
        Theme = CurrentSite.CultureName 
        Exit For 
       End If 
      End If 
     Next 
    End If 
End Sub 

在上面的代码, CurrentSite.CultureName将是一个语言文化名称(例如,en-gb或nn-no),它具有包含所有必需资源的现有相应主题文件夹。

页面确实有EnableTheming设置为True。另外,我尝试删除主题设置代码并在页面中使用Theme="en-gb"无效。

有没有什么立即明显的为什么网址没有解决?

回答

1

使用皮肤文件来做到这一点。改变你的形象标签:

<asp:Image runat="server" SkinID="SomeImage/> 

而在你App_Themes\MyTheme\文件夹,添加一个新的Skin文件(MyTheme.skin),并添加这样的:App_Themes\MyTheme\

<asp:Image runat="server" SkinID="SomeImage" ImageUrl="images\image.jpg"/> 

此图片的皮肤现在指向image.jpg夹。

对于动态图像,你可以在你的BasePage做到这一点(假设你有一个):

public string ThemePath { get { return "~/App_Themes/" + this.Theme + "/"; } } 

public string MapThemePath(string relativePath) 
{ 
    return string.Format("{0}{1}", this.ThemePath, relativePath); 
} 

但是,因为我看不到你实际上在做什么,我不能说这是推荐的解决方案。一般来说,你的主题只包含需要的东西或布局和显示。你在谈论动态图像,对我而言,听起来不像它属于主题?不确定,只是一个想法。

+0

对于每个图像?这是我主要关心的问题,因为有些图像是根据数据实时生成的。 – 2011-03-21 13:43:14

+0

好的 - 更新了我的答案。 – 2011-03-21 13:55:20

+0

感谢您的回答;这是我所推迟的,但看起来可能是唯一的解决方案 - 至少暂时是这样。 – 2011-03-23 21:38:33