Edit1 - 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.
Edit2 - 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
Edit3 - 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.
Edit3.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")]
public class MyExtension
Edit3.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);
}
Edit4 - Deploying the Extension
To deploy the extension, drop the class file into the "App_Code/Extensions" folder of your BlogEngine.NET install.
Edit5 - 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")]
public class StopLeechers
{
public StopLeechers()
{
ImageHandler.BeforeServing += new EventHandler<ImageHandler.FileHandlerEventArgs>(StopReferrers);
}
void StopReferrers(object sender, ImageHandler.FileHandlerEventArgs 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();
}
}
}
}