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



我目前正在MVC应用程序上设置一个亮/暗主题

它在一个页面上工作,但是当我更改页面或刷新页面时,样式表会返回到默认页面。

HTML

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

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>

捆绑配置

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"));
}
}

我需要更新我的家庭控制器或Bundle Config.cs文件吗?它确实在两个样式表之间发生了变化,但一旦刷新或我更改了视图,它就会回到Bundle配置文件中的默认值。

感谢提供的任何帮助

好的,所以您可以将此值存储为cookie。

当您使用c#时

以下是关于如何在c#中使用cookie的链接https://www.ryadel.com/en/asp-net-cookie-read-write-delete-c-sharp-tutorial-guide-how-to/

因此,cookie中的值存储原则是一个字符串化的json对象,因此您可以在其中设置更多信息

在服务器端

如果未设置cookie:

您创建了一个包含以下内容的cookie:"{主题:‘明亮’}">

如果设置了cookie,你什么都不会做。

如果用户更改主题,则:

您解析cookie内容,并将主题的值修改为"dark",再次将其字符串化,然后将其发送到客户端。

在客户端

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

最新更新