asp.net 派生母版页、页面标题和动态 CSS 链接

发布于 2024-10-21 13:07:29 字数 1032 浏览 0 评论 0原文

不确定派生页面是否确实与这里的问题相关,但在我目前正在处理的一些代码上遇到了一个有趣的问题。

我有一个自定义母版页类,它派生自 System.Web.UI.MasterPage,以便可以使用其他有用的属性对其进行扩展。

使用此母版页的页面在顶部有一个声明(请注意设置的页面标题)。

<%@ Page Language="C#" MasterPageFile="~/MasterPages/Landing.master" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" Title="Welcome to the site" %>

此外,母版页的头部有样式表引用,这些引用是从配置文件中定义的 CDN 中提取的。

<head id="Head1" runat="server">
    <link rel="stylesheet" type="text/css" href="<%= CDN %>/css/main.css" />
</head>

现在,上面的示例显然不起作用,因为头容器中的 runat 属性意味着链接中的代码块是静态文本,并在生成的 html 中按原样呈现。

如果我从 head 中删除 runat 属性,CDN 可以工作,但现在我注意到标题不再被设置。如果我调试并尝试在立即窗口中访问 Page.Title,则会出现异常:

// Using the Title property of Page requires a header control on the page. (e.g. <head runat="server" />).

那么,有没有办法从声明中获取页面标题,将我自己的标题占位符放在头部并设置它从母版页代码隐藏,或者,是否有更好的方法来动态设置样式表的 CDN 域? 我认为我能做到这一点的唯一方法是构建整个 html 链接标记和将其附加到标题控件,但我认为可能有更优雅的解决方案,所以我首先在这里询问。

Not sure if the derived page is actually relevant to the problem here, but ran into an interesting gotcha on some code I'm working through at the moment.

I have a custom masterpage class, which derives from System.Web.UI.MasterPage so that it can be extended with additional useful properties.

The page that that uses this masterpage has a declaration at the top (note the Page Title being set).

<%@ Page Language="C#" MasterPageFile="~/MasterPages/Landing.master" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" Title="Welcome to the site" %>

In addition, the master page has stylesheet references in the head which are pulled from a CDN that is defined in a config file.

<head id="Head1" runat="server">
    <link rel="stylesheet" type="text/css" href="<%= CDN %>/css/main.css" />
</head>

Now the example above obviously doesn't work, because the runat attribute in the head container means that the codeblock in the the link is static text, and renders as is, in the resulting html.

If I remove the runat attribute from head, the CDN works, but now I notice that the Title is no longer being set. If I debug, and try to access Page.Title in the Immediate Window, I get an exception:

// Using the Title property of Page requires a header control on the page. (e.g. <head runat="server" />).

So, is there a way to get the Page Title from the declaration, put my own title placeholder in the head and set it from the master page code-behind, or, is there a better way to dynamically set the CDN domain for the stylesheets? The only way I think I can do that is to build the entire html link tag(s) and append it to the header control, but I thought there might be a more elegant solution, so I'm asking here first.

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

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

发布评论

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

评论(2

不交电费瞎发啥光 2024-10-28 13:07:29

就 CDN 而言,我们构建了一个特殊的 Url.ResolveContentUrl() 方法,该方法与我们的配置相关,并根据运行模式等指向正确的样式表(以及任何其他静态资产)。因此,您的开发和 QA 在本地工作,然后您的生产到 CDN 时零代码更改。

另外,您应该将内容占位符放入母版页的部分。这是飞翔的唯一方法。

Insofar as the CDN goes, we build a special Url.ResolveContentUrl() method that spoke with our configuration and pointed at the correct stylesheets (and any other static asset) depending on running mode, etc. So your dev and QA work locally then your production goes to the CDN with zero code changes.

Also, you should put a content placeholder into the part of the masterpage. It is the only way to fly.

给我一枪 2024-10-28 13:07:29

在您的母版页中,您可以创建这样的例程:

Public Sub AddStyleSheetLink(ByVal fileName As String, Optional ByVal media As String = "all")
    Dim stylesheetLink As New HtmlLink
    With stylesheetLink
        .Attributes("href") = fileName
        .Attributes("type") = "text/css"
        .Attributes("rel") = "stylesheet"
        .Attributes("media") = media
    End With
    MasterHeader.Controls.Add(stylesheetLink)
End Sub

在您的 Page_Init (继承您的母版页的页面)中,这样的内容

CType(Master, MasterPage).AddStyleSheetLink(CND & "/css/main.css")

您可以动态添加您需要的内容,而不会弄乱您的 标签和页面标题。

In your masterpage, you can create a routine like this:

Public Sub AddStyleSheetLink(ByVal fileName As String, Optional ByVal media As String = "all")
    Dim stylesheetLink As New HtmlLink
    With stylesheetLink
        .Attributes("href") = fileName
        .Attributes("type") = "text/css"
        .Attributes("rel") = "stylesheet"
        .Attributes("media") = media
    End With
    MasterHeader.Controls.Add(stylesheetLink)
End Sub

And in your Page_Init (the page that inherits your masterpage), so something like this

CType(Master, MasterPage).AddStyleSheetLink(CND & "/css/main.css")

You can dynamically add what you need without messing with your <head> tags and page title.

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