根据编译配置更改母版页中 CSS 文件的位置

发布于 2024-09-06 15:37:25 字数 824 浏览 1 评论 0原文

Visual Studio 2010 有一个非常酷的新功能。您现在可以添加转换文件,这些文件将根据您正在编译的配置更改 web.config 文件。其结果是您可以为您的各种环境(例如:开发、登台、生产等)更改 web.config 文件。

对于 web.config 文件来说这一切都很好,但是其他可能需要如此细心照顾的页面又如何呢?例如,我们的母版页在不同的环境中指向不同的 CSS 文件位置(是的,我知道,这不是我的想法,我无法更改它)。有没有一种很酷和/或优雅的方式来进行改变?

我尝试使用

  <appSettings>
    <add key="pathPrefix" value="/OAFA/"  xdt:Transform="Replace" xdt:Locator="Match(key)"/>
   </appSettings>

然后在母版页中添加以下内容。 PathPrefix 是一个简单的属性,仅查询 Web 配置中的值。

    <script src="<%= PathPrefix %>scripts/jquery-1.3.2.js" type="text/javascript"></script>

这对于脚本来说非常有用,但对此却很糟糕......

    <link rel="Stylesheet" href="<%= PathPrefix %>css/main.css" />

在这种情况下,它只编码“<%=”和“%>”这远不理想。有什么想法吗?

Visual Studio 2010 has a new feature that is just way cool. You can now add transformation files that will change the web.config file based on the configuration you are compiling. The upshot of which is that you can change the web.config file for your various environments (eg: dev, staging, production, etc).

That's all fine and dandy for the web.config file but what about other pages that may need such tender care. Our master page, for example, points to different locations for it's CSS file in different environments (Yes, I know, it wasn't my idea and I can't change it). Is there a cool and/or elegant way to make the change?

I have tried to using

  <appSettings>
    <add key="pathPrefix" value="/OAFA/"  xdt:Transform="Replace" xdt:Locator="Match(key)"/>
   </appSettings>

and then add the following in the master page. PathPrefix is a simple property that justs queries the value in the web config.

    <script src="<%= PathPrefix %>scripts/jquery-1.3.2.js" type="text/javascript"></script>

This works great for the scripts but sucks rocks for this....

    <link rel="Stylesheet" href="<%= PathPrefix %>css/main.css" />

In this case it just encodes the "<%=" and "%>" which is far less than ideal. Any thoughts?

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

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

发布评论

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

评论(1

唯憾梦倾城 2024-09-13 15:37:25

将 ID 和 runat="server" 属性添加到链接中,如下所示:

<link id="StylesheetLink" runat="server"
      rel="stylesheet" href="stylesheet1.css" type="text/css" />

然后您可以在代码隐藏中以编程方式更改样式表的源代码,如下所示:

StylesheetLink.Attributes["href"] = "stylesheet2.css";

Add an ID and a runat="server" attribute to your link, like this:

<link id="StylesheetLink" runat="server"
      rel="stylesheet" href="stylesheet1.css" type="text/css" />

And then you can change the stylesheet's source programmatically in your codebehind like this:

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