all posts

Customizing Graffiti Chalk GetAppSetting

Published to Blog on 19 Jan 2009

Continued Graffiti goodness. Here is another Graffiti customization - the GetAppSetting Chalk extension. I recently put this one together so I haven’t grown to love it as much as the RedirectIfSinglePost extension, but it is very handy to have around when you need it.

What does it do?

You use it to pull a AppSettings configuration value from your web.config file. I think Graffiti’s Custom Fields are great and I use them all the time, but I’ve found that sometimes I just need access to a custom value that is the same for all posts or categories. Storing that value in the web.config makes a lot of sense and we ASP.NET developers are used to utilizing the AppSettings section. But I couldn’t find an easy way to access the web.config AppSettings inside my views. So I wrote a new Chalk extensions to do just that. This really isn’t all that powerful by itself, I could have just hard-coded a string in the view, but it’s really useful for storing something like an Amazon or Twitter API key that’s used in calls for other Chalk extensions.

The Code

namespace DanHounshell.Graffiti.Chalk
{
  [Chalk("ConfigurationAppSettings")]
  public class ConfigurationAppSettings
  {
    public string GetAppSetting(string setting)
    {
      return ConfigurationManager.AppSettings\[setting\];
    }
  }
}

Usage

Just place the following wherever you need it in your view markup:

$ConfigurationAppSettings.GetAppSetting("SomeAppSettingKey");

As I mentioned, I find this most powerful when mixing with other Chalk extension calls. Here’s an example of a fictional call to a Chalk extension that hits the Ebay Web Service to return 10 products that match the search term “dell laptop”. The GetAuctions method expects my Ebay API key. Rather than hard-coding it into my view I instead store in the AppSettings section of my web.config file and use the GetAppSetting call to pull it out:

$EbayService.GetAuctions("dell laptop",
  10,
  $ConfigurationAppSettings.GetAppSetting("MyEbayAPIKey"))

I hope you find this Graffiti Chalk extension useful. Please leave a comment if you have any suggestions or questions.


Dan Hounshell
Web geek, nerd, amateur maker. Likes: apis, node, motorcycles, sports, chickens, watches, food, Nashville, Savannah, Cincinnati and family.
Dan Hounshell on Twitter