Recent Articles

Localizing ASP.NET (Which Is Best?)
There are different approaches to localizing an ASP.NET application. You can use a global resource file or local ones. The local resource files only applies to a single page or user control, whereas the global can be...

ASP.NET Security: Remove The X-AspNet-Version...
I've always been a little annoyed by the fact that ASP.NET websites sends the version number as a HTTP header. For an ASP.NET 2.0 application this is added automatically to the headers and you cannot remove it from code.

ASP.NET: Don't Use The ThreadPool
I've always been a big fan of using the ThreadPool for asynchronous execution, but in ASP.NET it is not the best approach for multi-threading. I'm not writing about when threading is appropriate and the impact of multi-core or dual core machines when doing threading, but...

Top Application Security Vulnerabilities In...
Some of the most common and dangerous application security vulnerabilities that exist in ASP.NET Web-based applications come not from the C# or VB.NET code that make up its pages and service methods, but...

BlogEngine.NET 1.0 Officially Released
We released the first version of BlogEngine.NET for anyone to download and use. This is very exciting to me because I've spent many hours during the last couple of months designing and coding in my spare time.



Recent WebProNews Articles

Google Phone Speculation Runs Wild
We have speculated before about the potential for a Google phone and an ad-supported network to back it. Google may have an early version of the device ready to go. Even if a Google phone emerges soon, we...

Almost Half of Search Queries Are Repeats
Forty percent of all search queries are repeat queries from users trying to find information they have found before, according to a new study. But if there has been a change in search result rankings since the last time the...

Mobile Ads Show Google The Money
Google wants its software on more cellphones for the opportunity to present lucrative mobile ads, but the wireless companies have issues with Google's business terms. File this one under billionaires squabbling...

Jupiter Has Good Prognosis For Health Ads
Google has been touting a Jupiter Research report on health information searching and its recommendations for advertisers. It's been a month since we visited Google's Health Ads blog due to an amazingly ill-advised...

Google Talks A Good Game On Copyright
Google will be one of the firms supporting a complaint to be filed with the Federal Trade Commission about sports leagues and other content companies violate copyright law and the first amendment. When the Computer...


08.03.07


Making Your ASP.NET App Extendable

By Mads Kristensen

People have asked me how we build the extension model into BlogEngine.NET. There's nothing to it - really, there isn't.

You need one small class and 14 lines of code in the global.asax. That is all you need to make your ASP.NET application extendable. An extension is just a normal class that is somehow marked as being an extension. No magic is needed.

The scenario I liked best was one where you could just drop your custom extension into the App_Code folder and then it would just work. Reflection would take care of creating an instance of the extension, but we need a way to separate the extensions from all other classes in the App_Code folder. That's where we need the small class.

The class

I decided it would make most sense to have all custom extensions be decorated with a custom attribute called Extension and then let global.asax activate them when the application starts. That would make the class definition of a custom extension look like this:

[Extension("description")]
public class MyCustomExtension



To do that, we need to write a very small class that inherits from System.Attribute and it looks like this:

public class ExtensionAttribute : System.Attribute
{
  /// <summary>
  /// Creates an instance of the attribute and assigns a description.
  /// </summary>
  public ExtensionAttribute(string description)
  {
   _Description = description;
  }

  private string _Description;
  /// <summary>
  /// Gets the description of the extension.
  /// </summary>
  public string Description
  {
   get { return _Description; }
  }
}


It's a very simple class that just has a description property and that's it. The description property is not really needed to implement extensions, so you can leave it out if you'd like.

Global.asax

Now we have our custom attribute, so it's time to let global.asax use reflection to activate the extensions. This method iterates through all the types in the App_Code folder and when it finds a class that is decorated with the Extension attribute then it creates an instance of it.

void Application_Start(object sender, EventArgs e)
{
  Assembly a = Assembly.Load("__code");
  Type[] types = a.GetTypes();

  foreach (Type type in types)
  {
   object[] attributes = type.GetCustomAttributes(typeof(ExtensionAttribute), false);
   foreach (object attribute in attributes)
   {
    a.CreateInstance(type.FullName);
   }
  }
}


That's it. Now your ASP.NET application handles custom extensions made by who ever wants to write them. There are many ways to customize this implementation. For instance, you can put the reflection code in the Begin_Request method instead of the Application_Start to let your extensions act as an HttpModule.

Download a custom extension (zip)

Comments


About the Author:
Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/

About WebProASP
WebProASP is a collection of up to date tutorials and insightful articles designed to help ASP users of any skill level implement successful ASP systems and practices. ASP Strategies and Tactics for Business

WebProASP is brought to you by:

SecurityConfig.com NetworkingFiles.com
NetworkNewz.com WebProASP.com
DatabaseProNews.com SQlProNews.com
ITcertificationNews.com SysAdminNews.com
WebProASP.com WirelessProNews.com
CProgrammingTrends.com ITManagementNews.com




-- WebProAsp is an iEntry, Inc. publication --
iEntry, Inc. 2549 Richmond Rd. Lexington KY, 40509
2007 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal

archives | advertising info | news headlines | free newsletters | comments/feedback | submit article



ASP Strategies and Tactics for Business WebProASP News Archives About Us Feedback WebProASP Home Page About Article Archive News Downloads WebProWorld Forums Jayde iEntry Advertise Contact