Custom Site Branding with XSLT and MVC Controller Actions
Frequently, the clients I build applications for request the ability to customize their sites for their own clients. In the past, this might be accomplished by selecting a different CSS stylesheet for each client. That has the unfortunate drawback of trying to maintain styles across multiple copies. Another approach would be to generate style information in the page itself, but this doesn’t provide the benefit of caching style information on the client side.
The approach I’m using currently is parameterized XSL transforms that can generate a style sheet on the fly from a template via an MVC controller action.
After creating an empty MVC project and adding a HomeController and View/Home/Index.cs, I set up my layout as follows:
In this layout, I’ve split my stylesheets into two categories. Structure concerns itself with size and placement. Brand concerns itself with colors, but may also include typography.
The branding in this case isn’t a CSS file, it’s an XSLT file.
I’ve added parameters to the XSLT file that can be supplied at runtime to change the appearance. This content gets merged with the simple brand.xml in the controller.
The controller uses a customization class stored in the models folder. In this case, to simplify things, I’m hardcoding the values and basing the customization on host-headers. Feel free to pull the information from a database or base the customization on user role, etc.