Documentation

ASP.NET Integration

For ASP.NET c# to use the code below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
 
public class KitGUI
{
    public static string AccountKey
    {
        get { return System.Configuration.ConfigurationManager.AppSettings["KitGUI.AccountKey"]; }
    }
    static Regex contentIDRE;
    static KitGUI()
    {
        contentIDRE = new Regex("^[a-zA-Z]+[a-zA-Z0-9\\-_]*$", RegexOptions.Compiled);
    }
    public static string GetContent(string contentID, string contentType, string defaultContent)
    {
        var str = GetContent(contentID, contentType);
        if (string.IsNullOrEmpty(str))
        {
            return defaultContent;
        }
        return str;
    }
    public static string GetContent(string contentID, string contentType)
    {
        if (!contentIDRE.IsMatch(contentID))
        {
            throw new Exception("KitGUI content ID must begin with a letter ([A-Za-z]) and " +
            "may be followed by any number of letters, digits ([0-9]), hyphens (\"-\"), underscores (\"_\")");
        }
        HttpContext context = HttpContext.Current;
        HttpCookie kitguiCookie = context.Request.Cookies["kitgui"];
        bool editMode = (kitguiCookie != null && kitguiCookie.Value == "1");
        string url = "http://s3.amazonaws.com/kitgui/clients/" + AccountKey + "/ids/" + contentID + ".txt";
        string cacheKey = "kitgui-content-" + contentID;
        string content = "";
        if (!editMode)
        {
            content = context.Cache[cacheKey] as string;
        }
        if (editMode || content == null)
        { // invalidate the cache if in edit mode
            content = getUrlContents(url);
            context.Cache[cacheKey] = content;
        }
        if (editMode)
        {
            return "<div style=\"display:inline-block\" class=\"kitgui-id-" + contentID + " kitgui-content-type-" +
            contentType + "\">" + content + "</div>";
        }
        else
        {
            return content;
        }
    }
    static string getUrlContents(string url)
    {
        try
        {
            StringBuilder sb = new StringBuilder();
            byte[] buf = new byte[8192];
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream resStream = response.GetResponseStream();
            string tempString = null;
            int count = 0;
            do
            {
                count = resStream.Read(buf, 0, buf.Length);
                if (count != 0)
                {
                    tempString = Encoding.UTF8.GetString(buf, 0, count);
                    sb.Append(tempString);
                }
            } while (count > 0);
            return sb.ToString();
        }
        catch
        {
            return "";
        }
    }
}
 

For ASP.NET VB use the code below.

Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Text
Imports System.Net
Imports System.IO
Imports System.Text.RegularExpressions

Public Class KitGUI
	Public Shared ReadOnly Property AccountKey() As String
	  Get
	    Return System.Configuration.ConfigurationManager.AppSettings("KitGUI.AccountKey")
	  End Get
	End Property
	Shared contentIDRE As Regex
	Shared Sub New()
		contentIDRE = New Regex("^[a-zA-Z]+[a-zA-Z0-9\-_]*$", RegexOptions.Compiled)
	End Sub
	Public Shared Function GetContent(contentID As String, contentType As String, defaultContent As String) As String
		Dim str = GetContent(contentID, contentType)
		If String.IsNullOrEmpty(str) Then
			Return defaultContent
		End If
		Return str
	End Function
	Public Shared Function GetContent(contentID As String, contentType As String) As String
		If Not contentIDRE.IsMatch(contentID) Then
		  Throw New Exception("KitGUI content ID must begin with a letter ([A-Za-z]) and " & _
                  "may be followed by any number of letters, digits ([0-9]), hyphens (""-""), underscores (""_"")")
		End If
		Dim context As HttpContext = HttpContext.Current
		Dim kitguiCookie As HttpCookie = context.Request.Cookies("kitgui")
		Dim editMode As Boolean = (kitguiCookie IsNot Nothing AndAlso kitguiCookie.Value = "1")
		Dim url As String = "http://s3.amazonaws.com/kitgui/clients/" & AccountKey & "/ids/" & contentID & ".txt"
		Dim cacheKey As String = "kitgui-content-" & contentID
		Dim content As String = ""
		If Not editMode Then
			content = TryCast(context.Cache(cacheKey), String)
		End If
		If editMode OrElse content Is Nothing Then
			' invalidate the cache if in edit mode
			content = getUrlContents(url)
			context.Cache(cacheKey) = content
		End If
		If editMode Then
			Return "<div style=""display:inline-block"" class=""kitgui-id-" & contentID _
                        & " kitgui-content-type-" & contentType & """>" & content & "</div>"
		Else
			Return content
		End If
	End Function
	Private Shared Function getUrlContents(url As String) As String
		Try
			Dim sb As New StringBuilder()
			Dim buf As Byte() = New Byte(8191) {}
			Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
			Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
			Dim resStream As Stream = response.GetResponseStream()
			Dim tempString As String = Nothing
			Dim count As Integer = 0
			Do
				count = resStream.Read(buf, 0, buf.Length)
				If count <> 0 Then
					tempString = Encoding.UTF8.GetString(buf, 0, count)
					sb.Append(tempString)
				End If
			Loop While count > 0
			Return sb.ToString()
		Catch
			Return ""
		End Try
	End Function
End Class

To show the content on web forms:

 
<%=KitGUI.GetContent("my-content-id","HTML")%>
 
To show the content on Razor:
 
@(new HtmlString(KitGUI.GetContent("my-content-id","HTML")))
 
This is showing how to pull content named "my-content-id" of content type "HTML".
 
It gets interesting when you assign your content IDs based on URL then it becomes very easy to create templated pages. Your content is only existing once saved in edit mode.