问题描述:

I am trying to have 2 different themes (basically 2 different CSS). User has an option to choose either light or dark theme. If he chooses the light theme, I need to load the light themed CSS and if he chooses the dark theme, I need to load the dark themed CSS.

I tried this

.NET MVC: How to implement different page appearance per user?

but was not able to make it working because Url.Action was just generating the link and it was not returning the result from the Action method.

I have a layout page which all the pages in my website inherits and I somehow want to have this to be loaded in the Layout page.

The other approach is

http://forums.asp.net/t/1892421.aspx?Dynamic+Themes+in+MVC+Razor

Is it ok to set the css in HttpContext.Application["Theme"]?

The other approach I came across was setting the css file using javascript while loading the page.

Which approach do you think is better?

网友答案:

I would use the following way inside the layout page. Perhaps I would/could improve the "GetThemeName" method so that it returns the full link string. So that the layout page only contains layout features instead of code.

ex:

@switch (UserSettings.GetThemeName())
    {
        case "Red":
            <link rel="stylesheet" href="/Content/ThemeRed/css/default.css" />
            break;
        case "Blue":
            <link rel="stylesheet" href="/Content/ThemeBlue/css/default.css" />
            break;
        default:
            <link rel="stylesheet" href="/Content/Default/css/default.css" />
            break;
    }

the UserSettings.GetThemeName is a public static function which gets the users current "theme name" or "theme id"

相关阅读:
Top