以编程方式设置 ASP.NET 主题:图像路径

发布于 2024-10-22 06:01:25 字数 1333 浏览 2 评论 0原文

我们有一个多语言网站,或者是文化敏感的网站,现在需要对一些静态内容进行定位;为此,我使用主题,因为它似乎是实现我想要的最简单的方法,但我一生都无法获取图像。

我在代码隐藏中设置主题,起初认为这可能是问题所在,但检查后看起来我正在做正确的事情(在 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" 在页面中应用主题,但无济于事。

是否有任何明显的原因可以解释 URL 未解析的原因?

We have a multilingual site, or culture-sensitive, and some of the static content now needs to be targeted; for this I'm using themes as it seems the easiest way to achieve what I want, but I can't for the life of me get the images to pick up.

I'm setting the theme in code-behind, and thought at first that maybe this was the issue, but on checking up it looks like I'm doing the right thing (setting on Pre-Init).

I expect to be able to reference images using relative paths where App_Themes/ThemeName/ is automatically resolved, such as:

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

For whatever reason, however, the image isn't being pulled through at all.

This is the code we have in place for setting the theme (the only really relevant part, I'm sure, being the Theme = CurrentSite.CultureName, which is applied successfully):

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

In the above code, CurrentSite.CultureName would be a language culture name (for example, en-gb, or nn-no) that does have an existing corresponding theme folder containing all required resources.

Pages do have EnableTheming set to True. Also, I have tried removing the theme-setting code and applying the theme in the page using Theme="en-gb" to no avail.

Is there anything immediately evident as to why the URLs aren't resolved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是一片海 2024-10-29 06:01:25

使用皮肤文件来执行此操作。将您的图像标签更改为:

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

并在您的 App_Themes\MyTheme\ 文件夹中,添加一个新的皮肤文件 (MyTheme.skin) 并添加以下内容:

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

此图像皮肤现在指向 image.jpgApp_Themes\MyTheme\ 文件夹中的 code>。

对于动态图像,您可以在 BasePage 中执行此操作(假设您有一个):

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

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

但是,由于我看不到您实际在做什么,所以我不能说这是推荐的解决方案。一般来说,您的主题仅包含所需的内容或布局和显示。你说的是动态图像,对我来说,这听起来不属于主题?不确定,只是一个想法。

Use a Skin file to do this. Change your Image tag to:

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

And in your App_Themes\MyTheme\ folder, add a new Skin file (MyTheme.skin) and add this:

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

This image skin now points to image.jpg within the App_Themes\MyTheme\ folder.

For dynamic images you can do this in your BasePage (assuming you have one):

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

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

But, since I can't see what you're actually doing, I can't say this is the recommended solution. In general your theme contains only things needed or layout and display. You're talking about dynamic images which, to me, doesn't sound like it belongs in a theme? Not sure, just a thought.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文