1

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="&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!

6
  • Definitely strange. It should work. Have you tried without the (string) ? Commented Sep 13, 2011 at 17:13
  • yes I have. I was getting this weird bug on some pages without the string cast: stackoverflow.com/questions/3462211/… Commented Sep 13, 2011 at 17:20
  • check to make sure your <head> tag does not have runat="server" Commented Sep 13, 2011 at 17:21
  • excellent that seemed to have fixed it! I do not understand why though? according to this article w3schools.com/aspnet/aspnet_controls.asp the runat="server" should allow the server to process them instead of prohiniting it? Any extra information about why this fixed it would be helpful! Thank you again! Commented Sep 13, 2011 at 17:29
  • Awesome, I moved that to an answer. Commented Sep 13, 2011 at 17:32

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.