Using C# to dynamically generate CSS files

前端 未结 4 786
暖寄归人
暖寄归人 2021-01-05 10:07

I have a site that gets deployed to over a dozen clients. The main website has a base template, and each client has a client folder that overrides the colours. The problem i

相关标签:
4条回答
  • 2021-01-05 10:45

    Instead of a Web Control, you're likely better off creating a Generic Handler. This won't have the overhead that a web control has.

    1. In your handler, accept the clientID via querystring - this allows you to cache on a client level
    2. In your .master file, you can link to it < link src="MyCssHandler.ashx?ClientID=<%=ClientID%>" >

    In your handler you have a few ways to work with the CSS.

    1. Just have a bunch of response.write for the css, and put in relevant client values
    2. Create an external CSS file with it's own special identifier - maybe <% %>. You could then load all the client specific values in a NameValuePair Collection, and then loop through the external CSS file, parsing <% NAME %> and replacing with the correct value. Then response.write this file. More complicated true but it allows for a hell of a lot cleaner CSS files
    0 讨论(0)
  • 2021-01-05 10:53

    Write it out as a file and let the browser pick it up as it would any other CSS file.

    Making inline stylesheets prevents the client cache mechanism, forcing your CSS to be served up with every page.

    0 讨论(0)
  • 2021-01-05 10:56

    This is entirely possible. You might want to consider cascading your stylesheets too, so that the dynamic one imports a (presumably larger) static one.

    Asp.NET has support for themes, too, but to be honest CSS is much more powerful.

    Why are you thinking ASCX instead of ASPX though? I'd have thought that since one css file represents an entire response, it could all fit in a page.

    an ashx is probably the lightest, fastest form of handler you can implement, so you might want to look at that...

    Oh and make sure you get your caching parameters right!

    0 讨论(0)
  • 2021-01-05 11:11

    Another option to consider may just be to use a "CSS compiler" -- such as SASS, LESS or even HSS which may support handy constructs like "mixins" and including other files. This approach may allow a system that, while not dynamic, is easily configurable to your different client's needs.

    For instance, with a "CSS compiler" the entire color schema could be stored in a single file as exportable variables or templates (depends on "compiler") -- modify that file, "recompile"** and wham, new color-scheme interface is everywhere (SASS also supports math on colors -- such as Hue shifting). This may make the deployment/management of using static content feasible enough for your purposes.

    I use SASS (it fit my needs/style whereas LESS/HSS did not). I would not switch back unless I really, really had to (which is to say: uhh, never) -- SASS in SCSS mode also understands CSS syntax so you can micro-evolve or mix and match (LESS and HSS also work like this, but HSS only works with a stricter subset of CSS syntax). CSS compilers can also be used in conjunction templates engines (such as TT4) or take advantage of including dynamically generated files (not dynamic-dynamic as in the question, but dynamic in the sense that they come from some other data-source) if extra power is needed.

    While just normal CSS cascading and class names/selectors can go a long ways, I find it much easier to separate the "logical cascade" (CSS, where CSS/cascading is vital) and "geeze, I wish this worked like a template" (CSS compiler, which should handle cases where CSS/cascading is abused).

    ** Both SASS and LESS can monitor files and recompile them automatically for you. SASS even allows monitoring entire directories

    0 讨论(0)
提交回复
热议问题