New to Telerik UI for ASP.NET Core? Download free 30-day trial

Change Theme On The Client

Change Theme On The Client

The Step by Step guide below demonstrates how to allow the user to change the theme of the application. The approach demonstrated uses cookies to store the theme selected by the user. For a runnable example, refer to the GitHub repo on how to change the theme on the client.

Тhe _Layout.cshtml file provides a Kendo DropDownList for theme selection.

        .OptionLabel("Select Theme")
        .DataSource(source =>
            source.Read(read =>
                    read.Action("GetThemes", "Home");
            }).Events(ev => ev.Change("themeSelection"))
        .HtmlAttributes(new { style = "float: right" })

Upon selection of a theme, the selected theme value is passed to the controller action method.

    function themeSelection(e) {
        var selectedTheme = e.sender.dataItem();
            url: "/Home/SetTheme",
            data: { selection: selectedTheme.ThemeId },
            success: function (data) {
                window.location = data.url;

Within the action method the selected theme value is appended to a cookie.

    public IActionResult SetTheme(string selection)
        CookieOptions cookie = new CookieOptions();
        cookie.Expires = DateTime.Now.AddMinutes(10);

        Response.Cookies.Append("theme", selection, cookie);
        var returnUrl = Request.Headers["Referer"].ToString();
        return Json(new { result = "Redirect", url = returnUrl });

Setting the theme in the _Layout.cshtml file follows the requirements discussed in the Including Client-Side Resources article. The selected theme value is retrieved from the cookie and CDN urls for the selected theme are generated. An alternative approach, if stylesheets are stored within the application, would be to generate the url to the stylesheet for the selected theme.

        var specialThemes = new string[] { "nova", "bootstrap", "fiori", "material", "materialblack", "office365" };
        var sassThemes = new string[] { "default-v2", "bootstrap-v4", "material-v2" };
        var commonThemeName = "common";
        var mainHref = "";
        var isThemeSelected = Context.Request.Cookies.TryGetValue("theme", out string selectedTheme);

        if (!isThemeSelected)
            selectedTheme = "default";

        var themeHref = mainHref + selectedTheme + ".min.css";
        var commonThemeHref = mainHref + commonThemeName + ".min.css";
        if (specialThemes.Any(x => x == selectedTheme))
            commonThemeName += "-" + selectedTheme.Replace("materialblack", "material");

        if (sassThemes.Contains(selectedTheme) && selectedTheme == "custom")
            <link rel="stylesheet" href="~/css/styles/kendo.custom.css" />
        else if (sassThemes.Contains(selectedTheme))
            <link href=@themeHref rel="stylesheet" type="text/css" />
            <link href=@commonThemeHref rel="stylesheet" type="text/css" />
            <link href=@themeHref rel="stylesheet" type="text/css" />

See Also

In this article