开发者

Implementing CSS as a variable in the site.master page in ASP.NET MVC3

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 messa开发者_开发问答ge 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="&lt;%: (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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜