ASP.NET Default Themes

Can I create a default theme for an ASP.NET website?

For example, if I had a theme called "Default" and I selected a theme called "NewTheme" and I referenced a file that does not exist in "NewTheme" but exists in the theme "Default" , for example

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

Could this be taken from "/App_Themes/Default/Images/image.jpg" if it does not exist in "/App_Themes/NewTheme/Images/image.jpg" ?

Also, if the CSS class did not exist in "NewTheme", but it was in "Default," then can it take "Default"? In fact, I think it would be better if he first took all the styles by default, and then redefined any that NewTheme encountered.

I know that global links work similarly to this, because if ive chose the localization "es" , and the key does not exist in the webreference.resx.es file, but it works in webreference.resx , then it takes a value from there.

I think this would be important functionality for ASP.NET themes, as I can imagine different themes only having certain images, and some styles have changed. I can’t imagine that every image and every style is always completely different for each theme. And therefore, without this functionality, it will be a case of duplication of styles / images that I am not a fan of (for obvious reasons!).

+6
source share
2 answers

This feature is not built into ASP.NET. However, you can implement it quite easily:

  • Capture the HttpApplication.BeginRequest event in Global.asax or in the HTTP user module.
  • Look for queries with URLs under "/ App_Themes / NewTheme /".
  • Check if the HttpRequest.PhysicalPath file exists.
  • If the file does not exist, call HttpContext.RewritePath and replace “NewTheme” in the “Default” request URL.
+1
source

The default themes that you describe are not supported by ASP.NET. There are regular Themes and StyleSheetTheme s, but changing them dynamically is more useful at the page request level than for individual controls or static files.

You can create your own version of themes for static files by rewriting or routing URLs, but these are no longer Themes.

For controls such as <asp:Image> , you can override them and change properties such as ImageUrl based on which files exist in a certain hierarchy of theme folders. Then use tag matching to replace all instances of this control with a new one, without any markup changes.

FWIW, the BeginRequest event in Global.asax is called only for dynamic files in IIS (Cassini also calls it for statics). To support static in IIS, you will need an HttpModule , and you will also need to configure IIS to run in integrated mode.

+2
source

Source: https://habr.com/ru/post/907887/


All Articles