简体   繁体   English

在 MVC 应用程序上更新动态 css 样式表

[英]Updating dynamic css style sheet on MVC application

I am currently setting a light / dark theme on my MVC application我目前正在我的 MVC 应用程序上设置浅色/深色主题

It works on one page however when i change pages or refresh the page the stylesheet goes back to the default page.它可以在一个页面上工作,但是当我更改页面或刷新页面时,样式表会返回到默认页面。

HTML HTML

<input type="button" onclick="updateStyleSheet('bootstrap-grey')" value="Dark Mode">
                <input type="button" onclick="updateStyleSheet('bootstrap')" value="Light Mode">   

JS JS

  <script>
        function updateStyleSheet(filename) {

            newstylesheet = "Content/" + filename + ".css";
            if ($("#dynamic_css").length == 0) {
                $("head").append("<link>")
                css = $("head").children(":last");
                css.attr({
                    id: "dynamic_css",
                    rel: "stylesheet",
                    type: "text/css",
                    href: newstylesheet
                });
            } else {
                $("#dynamic_css").attr("href", newstylesheet);
            }

        }
    </script>

Bundle Config捆绑配置

 public class BundleConfig
    {

        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.validate*"));

           bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));

            bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                      "~/Scripts/bootstrap.js"));

            bundles.Add(new StyleBundle("~/Content/css").Include(
                      "~/Content/bootstrap.css",
                      "~/Content/site.css"));
        }
    }

Do i need to update my Home Controller or my Bundle Config.cs file?我需要更新我的 Home Controller 或我的 Bundle Config.cs 文件吗? It does change between the two style sheets however as soon as it is refreshed or i change the view it goes back to the default in the Bundle config file.它确实在两个样式表之间发生了变化,但是一旦它被刷新或我更改了视图,它就会回到 Bundle 配置文件中的默认值。

Any help is appreciated任何帮助表示赞赏

Ok, so you can store this value as a cookie.好的,所以您可以将此值存储为 cookie。

As you're working with c#当您使用 c#

Here is link about how to work with cookie in c# https://www.ryadel.com/en/asp-net-cookie-read-write-delete-c-sharp-tutorial-guide-how-to/这是有关如何在 c# https 中使用 cookie 的链接://www.ryadel.com/en/asp-net-cookie-read-write-delete-c-sharp-tutorial-guide-how-to/

So the principle, the value store in the cookie, is a json object stringified, so you can set more info inside it.所以原则,cookie中存储的值,是一个json object字符串化,所以你可以在里面设置更多信息。

In server side在服务器端

if no cookie is set:如果没有设置 cookie:

You create a cookie with content: "{theme:'light'}"您创建了一个 cookie,其内容为:“{theme:'light'}”

If a cookie is set you won't do anything.如果设置了 cookie,您将不会做任何事情。

If the user change of theme then:如果用户更改主题,则:

You parse the cookie content, and modify the value of theme to 'dark', stringify it again, and send it to the client.您解析 cookie 内容,并将 theme 的值修改为“dark”,再次对其进行字符串化,然后将其发送给客户端。

In client side :在客户端

You check the cookie, parse it and set the custom theme...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM