0 Comments

In BlogEngine.NET, all files and images that are inserted on a post will be served using an HttpHandler. Actually, they are served by FileHandler and ImageHandler respectively. They are almost identically and they both share the same event model. For the purpose of this example, I’ll stick with the file handler but the examples will work for both.

Events

There are three events on the file handler.

  • BeforeServing
  • FileServing
  • BadRequest

BeforeServing
This event is fired before a file is served, no matter if it exists on disk or not.

FileServing
This event is fired when the file exists on disk and the handler is serving it to the requesting client.

BadRequest
This event is fired when the requested file does not exist on disk and therefore cannot be served.

Uses

The events are not used anywhere by BlogEngine.NET. They are there for plug-ins to take advantage of. It would be quite easy to build an HttpModule that listens to the FileServing event and logs the request for statistical purposes. Another module could listen to BeforeServing, so it could stop the request if it comes from another website. That way you can prevent deep links to your files. The BadRequest event can be used to log non-existing files so you can fix broken links.

This is just some of the possibilities that the FileHandler's events can be used for.

The difference between the ImageHandler and FileHandler has something to do with client-side caching and content-types. Other than that they are alike.