在 ASP.NET MVC3 中将 CSS 实现为 site.master 页面中的变量

发布于 12-04 09:29 字数 1388 浏览 2 评论 0原文

我正在使用 ASP.NET MVC3 实现一个 Web 应用程序。在应用程序中,我想添加基于单击某些链接在 CSS 文件(对于主题)之间切换的选项。我做了一些研究,得出了以下结论:

  1. 要将数据传递给 site.master,最好的解决方案是创建一个抽象控制器并将其继承到以 site.master 作为模板的其他控制器中,根据本文: 传递数据到查看母版页
  2. I可以将 viewbag 消息传递到链接控制 css 文件 URL 以设置要使用的当前 css 文件,基于我看到传递到 site.master 页面顶部的脚本的代码:

script src="<%: Url.Content("~ /Scripts/jquery-1.5.1.min.js") %>" type="text/javascript"

因此,我使用以下方法创建了一个抽象控制器 ApplicationController:

public ApplicationController()
    {ViewBag.NewMessage = "../../Content/Site2.css";}

在 site.master 中,我包含了此链接:

<link href="<%: (string) ViewBag.NewMessage %>" rel="stylesheet" type="text/css" />

但是,这似乎不起作用,因为它被解释为:

<link href="&lt;%: (string) ViewBag.NewMessage %>" rel="stylesheet" type="text/css" />

只有当我删除引号时:

<link href=<%: (string) ViewBag.NewMessage %> rel="stylesheet" type="text/css" />

它是否被正确解释(并且网页使用正确的CSS呈现),除了它违反了html标准:

<link href=../../Content/Site2.css rel="stylesheet" type="text/css" />

关于如何解决这个问题的任何建议或者是否有更优雅的解决方案我失踪了?我将继续在 ApplicationController 中实现变量,这些变量根据用户在页面顶部单击的链接进行选择,以在 css 样式之间切换。

谢谢你!

I am implementing a web application using ASP.NET MVC3. In the application I want to add the option of switching between CSS files (for themes) based on clicking some links. I did some research and I came up with the following:

  1. To pass data to site.master, the good solution is to create an abstract controller and inherit that into the other controllers that have site.master as their template, according to this article: Passing Data to View Master Pages
  2. I can pass a viewbag message to the link which controls the css file URL to set the current css file to use, based on the code that I am seeing being passed to scripts at the top of the site.master page:

script src="<%: Url.Content("~/Scripts/jquery-1.5.1.min.js") %>" type="text/javascript"

So I created an abstract controller, ApplicationController, with the following method:

public ApplicationController()
    {ViewBag.NewMessage = "../../Content/Site2.css";}

And in the site.master, I included this link:

<link href="<%: (string) ViewBag.NewMessage %>" rel="stylesheet" type="text/css" />

However, that doesn't seem to be working, as it is being interpreted as:

<link href="<%: (string) ViewBag.NewMessage %>" rel="stylesheet" type="text/css" />

And only when I remove the quotation marks:

<link href=<%: (string) ViewBag.NewMessage %> rel="stylesheet" type="text/css" />

is it being interpreted correctly (and the webpage gets rendered with the correct css), except that it violates html standards:

<link href=../../Content/Site2.css rel="stylesheet" type="text/css" />

Any suggestion about how to go tackling this problem or is there a more elegant solution I'm missing? I was going to proceed and implement variables in the ApplicationController that get selected based on links the user clicks at the top of the page, to switch between css styles.

Thank you!

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

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

发布评论

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

评论(1

别低头,皇冠会掉2024-12-11 09:29:58

检查并确保您的 标记没有 runat="server"

进行此更改后,请务必检查您的脚本和 css 标签。如果您使用 ~/ 引用应用程序根目录,此更改可能会破坏路径。要解决此问题,请使用 Url.Content(...) 帮助器。

Check to make sure your <head> tag does not have runat="server".

After making this change, be sure to check your script and css tags. This change can break the paths if you use the ~/ to ref app root. To help with this, use the Url.Content(...) helper.

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