Access the page from a BlogEngine.NET extension

Jun 2, 2008

Recently, I’ve been getting a lot of questions from people who ask how to manipulate the page from an extension in BlogEngine.NET. Most of the times the purpose has been to add a stylesheet or JavaScript reference in the page header dynamically.

It’s actually pretty easy but not that obvious so I thought I’d give an example. The following code inserts a stylesheet into the page header from an extension. Remember that the stylesheet must be placed in your theme folder.

Example


using System;

using System.Web;

using System.Web.UI.HtmlControls;

using BlogEngine.Core.Web.Controls;

using BlogEngine.Core;

 

[Extension("Test description", "1.0", "Mads Kristensen")]

public class PageTest

{

  public PageTest()

  {

    // Registers event handlers for serving both posts and pages.

    Post.Serving += new EventHandler<ServingEventArgs>(ServingHandler);

    Page.Serving += new EventHandler<ServingEventArgs>(ServingHandler);

  }

 

  private void ServingHandler(object sender, ServingEventArgs e)

  {

    HttpContext context = HttpContext.Current;

    if (context != null && !context.Items.Contains("PageTest"))

    {

      // Gets a reference to the serving page.

      System.Web.UI.Page page = context.CurrentHandler as System.Web.UI.Page;

      if (page != null)

      {

        // Creates a stylesheet link reference.

        HtmlLink link = new HtmlLink();

        link.Href = "style.css";

        link.Attributes.Add("type", "text/css");

        link.Attributes.Add("rel", "stylesheet");

 

        // Adds the stylesheet to the header of the page.

        page.Header.Controls.Add(link);

 

        // Sets a flag so only one stylesheet is added

        context.Items.Add("PageTest", 1);

      }

    }

  }

}

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Comments (3) -

Dave
Dave Switzerland
6/5/2008 11:38:07 PM #

Hi Mads!

I just wrote my first extension for BlogEngine.NET - a tabs extension which makes use of javascript and css. I tried to include a css file but I couldn't figure out, how to do that, when locating the css file in the extensions folder. Neither a direct link to the file nor using the css.axd worked. Also using a webresource doesn't work because you have to insert it in AssemblyInfo.cs. So how can I accomplish that - copying the extensions.cs, css and javascript files in the same folder without making changes in other files or copying these files into different locations?

Thanks for your great work on BlogEngine.NET!
Dave

Juan
Juan Argentina
6/25/2008 2:26:04 PM #

Offtopic question Mads... which code formatter are you using? CSharpFormatter (which comes with blogengine) doesn't highlight class names like that...

Care to share it?

Chris
Chris United States
8/6/2008 4:22:40 AM #

Could you post an example of how to insert JavaScript at the bottom of the page?  This would be useful for script that needs to load after everything else.

Thanks!

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.