How to Enable Theme Customization Dynamically in ASP.NET MVC?

By: Jignasha
Updated: June 19, 2019 | Technology: Asp.Net, ASP.NET Core, Asp.Net MVC, Theme Customization

Introduction for Theme Customization in ASP.NET MVC:

In ASP.NET MVC Theme Customization Dynamically, User able to customize the theme on the website through custom theme’s settings from the admin panel to change the theme color, backgrounds, side panels, custom menus, portfolios and more functionality of the website using LESS file in ASP.NET MVC.

For example:

To set two types of colors, one is a light colored theme and another is a dark colored theme. User able to change theme color using LESS file and it will be reflected in the whole theme in ASP.NET MVC.

Here we are using LESS file for theme customization in ASP.NET MVC.

Here we are using LESS file for theme customization in ASP.NET MVC.

LESS is a dynamic cascading style sheet (CSS). It can be compiled and converted into CSS and run on the client side or server side.

It is basically used for theme customization which means one theme with various colors. LESS file supports variable declarations and uses that variable in CSS. We can also add CSS classes in less file. It also supports CSS classes.

Tools & Packages Needed for setting up a theme in Microsoft .NET MVC:

  1. Microsoft Visual Studio
  2. NuGet Microsoft ASP.NET Web Optimization package
  3. NuGet dotless package

Implementation in ASP.NET MVC:

We had implemented Theme customization in ASP.NET MVC. There are some steps for implementation using LESS file theme customization.

We need to make one variables.less file which stores all variables that are used in the less file. There are two kinds of color light and dark. We can also use any one of them in the LESS file. There are many inbuilt functions for customization dynamically in .NET MVC.

For LESS File Implementation Example:

We have to declare the following variables.

Need to add a custom.less file. we want above variables in the custom.less file. but we need to import variables.less file on top.

Now By changing variables value in variables file. It will reflect changes in CSS files.

But For that, we have to compile LESS file. We can compile it using visual studio feature Web Essentials.

It is not feasible for users to compile it every time in ASP.NET MVC.

There is a NuGet package called dotless. It will compile all less files dynamically and reflect changes of Theme. You need to install dotless package of Nuget in your ASP.NET MVC application.

Create LessTransform class in App_Start directory of your application.

Now, Install Optimization package of NuGet for bundling and minifications of CSS and JS.

After installing package there will be a file called BundleConfig.cs in App_Start directory.

You need to create a bundle and add CSS, LESS file in BundleConfig like following. You need to maintain sequence here.

Add variables.less at first, thereafter add a custom.less file in the minification.

Now, add this bundle in MVC view.

Make sure that debug tag is false in web.config file of your asp.net mvc application.

Now, To make this user-friendly we need to add customization page in admin.

It will read variables from LESS file. user can update it from customization page.

We need to update variables in LESS file When a user clicks on the save button.

By changing variables file it will automatically change colors of theme based on variables.

Issues in this approach:

  • Sometimes we need to delete cache to reflect changes in the .NET application.
  • Make sure that there is no syntax error in any CSS or LESS file. It will create issues in ASP.NET MVC theme customization.

Know more about ASP.NET MVC tips and tricks for your everyday problems faced as an ASP.NET MVC developer.

We have teams of ASP.NET MVC developers. We will be happy to help you!

Book Free consultation Call