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

   1:  namespace DanHounshell.Graffiti.Chalk 
   2:  {
   3:     [Chalk("ConfigurationAppSettings")]
   4:     public class ConfigurationAppSettings 
   5:     {
   6:        public string GetAppSetting(string setting) 
   7:        {
   8:           return ConfigurationManager.AppSettings[setting];
   9:        }
  10:     }
  11:  }

Usage

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

   1:  $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:

   1:  $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, mobile, motorcycles, watches, food, Nashville, Savannah, Cincinnati and family. Dislikes: mean people
Dan Hounshell on Twitter


  • On 27 Jan 2009 Digging My Blog - Dan Hounshell said:

    My last Customizing Graffiti post was a simple little thing. This one is just as simple - the GetQuerystringValue

  • On 1 Feb 2009 Digging My Blog - Dan Hounshell said:

    In my last post, Customizing Graffiti: Chalk GetQuerystringValue , I promised that I was brewing up a