【问题标题】:How to set stylesheet programmatically in an ASP.NET MVC 2 project?如何在 ASP.NET MVC 2 项目中以编程方式设置样式表?
【发布时间】:2011-01-21 08:05:55
【问题描述】:

所以..这是我在 MVC2 项目中的 site.master ASPX 文件的上半部分:

<head runat="server">
   <link href="<%= ViewData[SomeNamespace.StyleSheetKey]; %>" rel="stylesheet" type="text/css" />
</head>
<div foo="<%= (string) ViewData[SomeNamespace.StyleSheetKey] %>">bar</div>

现在 div 标签正确地呈现了样式表名称,但链接标签中的那个在它被写入时被呈现,没有被解释。另外添加了一个路径前缀。

所以 ASP.NET 引擎似乎想处理链接标记中 href- 参数中的文本,“帮助”我为我的 .css 文件添加正确的相对路径前缀。

我现在如何能够以编程方式设置样式表的名称?

【问题讨论】:

  • 我现在这是一个很长的镜头,但请尝试从 中删除 runat="server" 标记
  • 您先生,是赢家 :-) 将其发布为答案,而不是评论!

标签: css asp.net-mvc master-pages


【解决方案1】:

好的,尝试从 &lt;head&gt; 标记中删除 runat="server" 标记

【讨论】:

    【解决方案2】:

    这会起作用

     <link href="<%= "" + ViewData[SomeNamespace.StyleSheetKey] %>" rel="stylesheet" type="text/css" />
    

    但这不是

      <link href="<%= (string)ViewData[SomeNamespace.StyleSheetKey] %>" rel="stylesheet" type="text/css" />
    

    或者正如 The_Butcher 所说,从标题中删除 runat="server"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-07
      • 1970-01-01
      • 2011-02-25
      • 2017-03-11
      • 2012-04-12
      • 2011-03-15
      相关资源
      最近更新 更多