【问题标题】:using code blocks in head content and link tags在头部内容和链接标签中使用代码块
【发布时间】:2011-01-11 18:43:02
【问题描述】:

我正在使用主题构建一个 asp.net 应用程序,并且我已经使用网络配置为该应用程序分配了一个主题。

我有一个要用于我的页面的书签图标,它位于主题目录中,但我无法从标题中的链接标记引用主题位置。

首先我尝试在链接标签href元素中放置一个代码块,但没有成功。相反,它所做的只是对

<link rel="shortcut icon" href="/App_Themes/<%=Page.Theme %>/images/bookmark.ico" type="image/x-icon" runat="server"/>

虽然我可以将代码块放在 hr 标签中的元素内,所以我不知道为什么它在链接标签中不起作用:

<hr test="<%=Page.Theme %>"/>  

然后我尝试在 head 标记内执行 Response.Write,但我收到一条错误消息,提示无法修改 Controls 集合,因为该控件包含代码块:

<% Response.Write("<link rel=\"shortcut icon\" href=\"/App_Themes/" + Page.Theme + "/images/bookmark.ico\" type=\"image/x-icon\"/>"); %>

我也用字符串文字尝试过,得到了同样的错误:

<%= "<link rel=\"shortcut icon\" href=\"/App_Themes/" + Page.StyleSheetTheme + "/images/bookmark.ico\" type=\"image/x-icon\"/>" %>

有没有办法从链接标签内的主题目录中引用一些东西?

我正在尝试在 ASP.NET 2 和 ASP.NET 2 MVC 应用程序中执行此操作。

【问题讨论】:

  • 您能否向我们展示您的第一个示例中呈现给浏览器的内容?如果您的应用程序存储在子目录中,这将因为您的链接指向根目录而爆炸。如果这不是问题,那么呈现给浏览器的 html 可能会持有密钥。
  • NickLarsen - 它将在指定我的代码块的地方呈现 <%=Page.Theme%>。

标签: asp.net asp.net-mvc asp.net-mvc-2 themes link-tag


【解决方案1】:

这不起作用,因为您将其标记为 runat="server"

试试这个

<link rel="shortcut icon" href="<%=ResolveUrl(string.Format("~/App_Themes/{0}/images/bookmark.ico", Page.Theme)) %>" type="image/x-icon"/>

【讨论】:

  • 也不起作用。仍然报错Controls collection cannot be modified because the control contains code blocks (i.e.)。
  • 你能告诉我你的整个 标签是什么样子的吗?
  • 如果你的 head 标签中有 runat="server" 并且你添加了任何其他控件,你就不能按照我的建议去做
  • 你知道为什么 runat="server" 让它失败吗?我认为这将是相反的方式
  • 标记为 runat="server" 的标签无法内联修改,因为 ASP.Net 正在管理该控件。
【解决方案2】:

杰里米,

尝试创建一个 html 帮助器,例如:

public static string SetThemeIcon(this HtmlHelper html, string themename)
{
    var filePath = VirtualPathUtility.ToAbsolute("~/App_Themes/" + themename + "/images/bookmark.ico");
    return "<link rel=\"shortcut icon\" href=\"" + filePath + "\" type=\"image/x-icon\"/>";
}

然后,在您的视图或母版页中,简单地引用它:

<%= Html.SetThemeIcon("test") %>

或如您上面的情况(在 mvc 中):

<%= Html.SetThemeIcon(String.IsNullOrEmpty(Page.Theme) ? Page.StyleSheetTheme : Page.Theme) %>

希望这会有所帮助...

吉姆

【讨论】:

    【解决方案3】:

    您可以通过使用一些服务器元素包装脚本来摆脱内联代码:

    <head runat="server">
       <title>My Test App <title>
       <div runat="server">
          <link rel="shortcut icon" href="/App_Themes/<%=Page.Theme %>/images/bookmark.ico" type="image/x-icon" runat="server"/>
       </div>
    </head>
    

    【讨论】:

    • 看来您可以将 runat=server 放在元素上,但您不能将绑定标签嵌入到 href 属性的中间,因为它们会被渲染为编码的 html。如果我在开头放一个 ~,它确实会渲染应用程序的根目录。
    【解决方案4】:

    我用这种方式。 (将虚拟空间添加到块中):

    <link href="<%=""+RootUrl%>_ui/css/font-awesome.min.css" rel="stylesheet"> <link rel="shortcut icon" href="<%=""+RootUrl%>_ui/favicon.ico" />

    【讨论】:

      【解决方案5】:

      好的,我通过使用一些内联代码让它工作了。内联代码不是我的第一选择,但我想要一个也适用于 mvc 的解决方案。这是我想出的:

      <head runat="server">
          <title>My Test App <title>
          <script language="CS" runat="server">
              void Page_Load(object sender, System.EventArgs e) 
              {
                  string sTheme = String.IsNullOrEmpty(Page.Theme) ? Page.StyleSheetTheme : Page.Theme;
                  litFacIcon.Text = "<link rel=\"shortcut icon\" href=\"/App_Themes/" + sTheme + "/images/bookmark.ico\" type=\"image/x-icon\"/>";
              } 
          </script>
          <asp:Literal ID="litFacIcon" runat="server"></asp:Literal>
      </head>
      

      【讨论】:

        【解决方案6】:

        你也可以这样做:

        <head>
            <style type="text/css">
                @import "<%= ResolveUrl("~/content/styles.css") %>";
                @import "<%= ResolveUrl("~/content/print.css") %>" print;
            </style>
        </head>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-19
          • 2022-06-18
          • 2014-10-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多