Extensions

Table of Contents

1 - Overview
2 - Supported Events
3 - Anatomy of Extensions
4 - Deploying the Extension
5 - Complete Example

1 - Overview

As of version 1.2, BlogEngine.NET supports extensions. Extensions allows you to write a class that can hook up to all the various events that are exposed in the application in a very simple manor.

At the highest overview, you create a class, decorate it with the "Extension()" attribute, create a default constructor hook up the events you want the extension to use, and add functionality to those events. After your extension is written, just drop the class file into the "App_Code/Extensions" folder of your BlogEngine.NET install and (if you did everything right) it will work.

2 - Supported Events

Below is a list of supported events that you can use in your extension. This list may change when version 1.2 is released.
  • Post.AddingComment
  • Post.CommentAdded
  • Post.RemovingComment
  • Post.CommentRemoved
  • Post.Rated
  • Post.Saving
  • Post.Saved
  • Post.Serving
  • Post.MarkedDirty
  • Page.Saving
  • Page.Saved
  • Page.Serving
  • Page.MarkedDirty
  • Comment.Approving
  • Comment.Approved
  • Comment.Serving
  • Category.Saving
  • Category.Saved
  • Category.MarkedDirty
  • Search.Searching
  • Search.IndexBuilding
  • Search.IndexBuild
  • BlogSettings.Changed
  • ReferrerModule.ReferrerRegistered
  • Utils.EmailSent
  • FileHandler.Serving
  • FileHandler.Served
  • FileHandler.BadRequest
  • ImageHandler.Serving
  • ImageHandler.Served
  • ImageHandler.BadRequest
  • Pingback.Sending
  • Pingback.Sent
  • PingbackHandler.Received
  • PingbackHandler.Rejected
  • PingbackHandler.Accepted
  • PingbackHandler.Spammed
  • Trackback.Sending
  • Trackback.Send
  • TrackbackHandler.Received
  • TrackbackHandler.Rejected
  • TrackbackHandler.Accepted
  • TrackbackHandler.Spammed

3 - Anatomy of Extensions

At the highest overview, you create a class, decorate it with the "Extension()" attribute, create a default constructor hook up the events you want the extension to use, and add functionality to those events.

3.1 - "Extension" Attribute

For your extension to be recognized by BlogEngine.NET, you have to decorate it with the "Extension()" attribute.

[Extension("This is a short description", "1.0", "Author Name")]
public class MyExtension

3.2 - Default Constructor

An Extension needs a default constructor– one without parameters. That constructor will be called when the web application starts. It is in the constructor that you hook the events up to your extension.

public MyExtension()
{
    Post.Saving += new EventHandler<SavedEventArgs>(Post_Saving);
}

4 - Deploying the Extension

To deploy the extension, drop the class file into the "App_Code/Extensions" folder of your BlogEngine.NET install.

5 - Complete Example

A complete example of an extension:

using System;
using System.Web;
using BlogEngine.Core.Web;
using BlogEngine.Core.Web.Controls;
using BlogEngine.Core.Web.HttpHandlers;
 
[Extension("Stops other websites in displaying your images on their own website", "1.0", "Author Name")]
public class StopLeechers
{

  public StopLeechers()
  {
    ImageHandler.Serving += new EventHandler<EventArgs>(StopReferrers);
  }

  void StopReferrers(object sender, EventArgs e)
  {
    HttpContext context = HttpContext.Current;
    if (context.Request.UrlReferrer != null)
    {
      if (context.Request.UrlReferrer.Host != context.Request.Url.Host)
      {
        context.Response.StatusCode = 403;
        context.Response.End();
      }
    }
  }
}

Last edited Mar 7, 2009 at 10:22 PM by BenAmada, version 1

Comments

angarato_surion Nov 28, 2012 at 8:46 PM 
if yu dont have a problemhaving it without gui you could easily make it's settings to an xml or to the database :P

HelloJonnyOh Oct 19, 2011 at 4:21 PM 
Where can I find instruction on how to make an extension with settings to it?