Question

Is it possible to create a default theme for an ASP.NET Website?

For example, If I had a theme called "Default", and ive selected a theme called "NewTheme" and I referenced a file which doesn't exist in the "NewTheme" but does exist in the "Default" theme like:

<asp:image id="img" runat="server" ImageUrl="~/Images/image.jpg" />

Could that then be taken from "/App_Themes/Default/Images/image.jpg" if it does not exist at "/App_Themes/NewTheme/Images/image.jpg"?

Furthermore if a CSS class didn't exist in "NewTheme", but it did in "Default", then could it take the "Default"? In fact, I think it would be better if it first took all the default styles, and then overrides any that "NewTheme" have which clashes.

I know Global References work similar to this because if ive selected "es" localization, and a key doesn't exist in the webreference.resx.es file but it does in webreference.resx, then itll take the value from there.

I think this would be important functionality for ASP.NET Themes as I can imagine different themes only having certain images changed, and certain styles changed. I can't imagine every image and every style always being totally different for each Theme. And therefore without this functionality, its going to be a case of duplicating styles/images, which I'm not a fan of (for obvious reasons!).

Was it helpful?

Solution

This functionality isn't built into ASP.NET. Nevertheless, you could implement it fairly easily:

  1. Hook the HttpApplication.BeginRequest event in Global.asax or in a custom HTTP module.
  2. Look for requests with URLs under "/App_Themes/NewTheme/".
  3. Check whether the file at HttpRequest.PhysicalPath exists.
  4. If the file doesn't exist, call HttpContext.RewritePath and replace "NewTheme" in the request URL with "Default".

OTHER TIPS

Default themes as you describe aren't supported by ASP.NET. There are regular Themes and StyleSheetThemes, but changing them dynamically is more useful at the Page request level than for individual Controls or static files.

You can code up your own version of themes for static files using URL rewriting or routing -- but then it's not really Themes any more.

For controls like <asp:Image>, you could override them and modify properties such as ImageUrl based on which files exist in some hierarchy of "theme" folders. Then use tag mapping to replace all instances of that control with the new one, without requiring any markup changes.

FWIW, the BeginRequest event in Global.asax is only invoked for dynamic files in IIS (Cassini calls it for statics, too). To support statics in IIS, you'll need an HttpModule, and you'll also need to configure IIS to run in Integrated mode.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top