Add CSS from Extension to all pages?

Topics: ASP.NET 2.0, Controls
Mar 17 at 6:30 PM
Hi,

I want to add a CSS-Stylesheet via an extension to the page header (Link-Tag).

I tried this:
            Post.Serving += Publishable_Serving;
            BlogEngine.Core.Page.Serving += Publishable_Serving;
//...
            LiteralControl ltr = new LiteralControl();

            string path = //...some file
            ltr.Text = string.Format("<link  type=\"text/css\" rel=\"stylesheet\" href=\"{0}\"></link>", path);

            page.Header.Controls.Add(ltr);
But this approach has two problems:
  1. The CSS gets added for each rendered Post on the page
  2. It does not work when doing a search ...
I know why this all happens, but what I am missing is a way to always add the HTML-Tag once to the header, for each Request involving HTML output.
Coordinator
Mar 17 at 7:45 PM
From MediaElementPlayer extension:
private static void AddStylesheet(string href, Page page)
{
  BlogEngine.Core.Web.Scripting.Helpers.AddStyle(page, href);
}
Mar 17 at 8:57 PM
Edited Mar 17 at 9:15 PM
Thanks for your anwser. I tried it this way before, same problem.
As soon as I navigate to BlogEngine.NET/contact Or BlogEngine.NET/search?q=something there is no StyleSheet present.
Other pages/posts work fine, except the root page, list of posts, adds the css multiple times...)
I am using the latest version from git master.

Edit:
Here is a real life example
http://ancientbyte.software
Choose the white layout on the top right ... if the layout changes back to red, the CSS has not been added (I added the red css as a hardcoded fallback atm)

Click on Contact or a Tag in the TagCloud ...
Coordinator
Mar 17 at 9:45 PM
Contact or search are not "publishable" pages. That "serving" event is part of IPublishable interface, it won't and should not work outside regular post or page.
Mar 17 at 9:50 PM
Edited Mar 17 at 10:48 PM
So, it's not possible to do this with an extension?
Guess I have to alter the BlogEngine.NET code then to make this possible?

Edit:
Okay, I found a way:
Added a Pipeline class to add Action<T> to a List.

This list will be executed on Page_PreRender of a MasterPage.

And with
ContentPlaceHolder c = page.FindControl("HeadContent") as ContentPlaceHolder;
LiteralControl ltr = new LiteralControl();
---
c.Controls.Add(ltr);
I am able to add the CSS just once to my Page.

Extension CSSSwitch is born ... somehow
Marked as answer by Rabite on 3/17/2016 at 2:45 PM