Click here to Skip to main content
15,886,689 members
Articles / Web Development / IIS

Adding a ProcessContext to ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.82/5 (14 votes)
11 Apr 20058 min read 68.7K   545   46  
A simple and reliable way to build context into a multi stage process in ASP.NET.
using System;
using System.Web;
using System.Web.UI;
using System.Collections;

namespace Base4.Web.UI
{
	/// <summary>
	/// This class allows you to move data around (without using a database) between Postbacks and Redirects
	/// that are part of the same process and working with the same data.
	/// </summary>
	public class ProcessContext
	{
		public static ProcessContext Current
		{
			get
			{
				//The overhead of creating a ProcessContext is minimal since it is just a proxy to the real data
				//the reason we cache it in the HttpContext is that if we construct a new ProcessContext on each
				//Call to ProcessContext.Current then we call RegisterHiddenField more than once per request
				//which is dangerous... as we may end up storing our data against the wrong ID!
				ProcessContext current = HttpContext.Current.Items["__PROCESSCONTEXT"] as ProcessContext;
				if (current == null)
				{
					current = new ProcessContext();
					HttpContext.Current.Items["__PROCESSCONTEXT"] = current;
				}
				return current;	
			}
		}
		public static string CurrentProcessID
		{
			get
			{
				return Current.ProcessID;
			}
		}
		protected string _processID = null;
		protected Hashtable _items = null;

		/// <summary>
		/// When the ProcessContext is constructed so postbacks function okay without a QUERYSTRING
		/// redirect this Registers a Hidden Field to store the ProcessID in the form.
		/// </summary>
		protected ProcessContext()
		{
			(HttpContext.Current.Handler as Page).RegisterHiddenField("__PROCESSID",ProcessID);
		}
		/// <summary>
		/// Accesses the Hashtable associated with this ProcessContext.
		/// It creates a new Hashtable if not found. The Hashtable is stored in the users Session
		/// keyed on the ProcessContext.ProcessID
		/// </summary>
		public Hashtable Items
		{
			get
			{
				if (_items == null)
				{
					_items = HttpContext.Current.Session[ProcessID] as Hashtable;
					if (_items == null) 
					{
						_items = new Hashtable();
						HttpContext.Current.Session[ProcessID] = _items;
					}
				}
				return _items;
			}
		}
		/// <summary>
		/// Figures out what the ProcessID of the current ProcessContext is
		/// Checking in order -> QueryString, __PROCESSID field, or creating a new GUID
		/// </summary>
		public string ProcessID
		{
			get
			{
				if (_processID == null)
				{
					_processID = HttpContext.Current.Request.QueryString["ProcessID"] as string;
					if (_processID == null)
					{
						_processID = HttpContext.Current.Request.Form["__PROCESSID"] as string;
						if (_processID == null)
						{
							_processID = Guid.NewGuid().ToString();
						}
					}
				}
				return _processID;
			}
		}
		/// <summary>
		/// Convenience access to this.Items[key] using this[key]
		/// </summary>
		public object this[object key]
		{
			get
			{
				return Items[key];
			}
			set
			{
				Items[key] = value;
			}
		}
		/// <summary>
		/// Used to discard all data associated with this process
		/// </summary>
		public void Discard()
		{
			HttpContext.Current.Session.Remove(ProcessID);
		}
		/// <summary>
		/// Used to empty the hashtable associated with this process
		/// </summary>
		public void Clear()
		{
			Items.Clear();
		}
		/// <summary>
		/// Used to decorate a URL with the ProcessID so the ProcessContext
		/// can be transfered to that URL
		/// </summary>
		/// <param name="url"></param>
		public string AttachProcessToUrl(string url)
		{
			if (url.IndexOf("?") == -1) 
				url = url + "?";
			else
				url = url + "&";
			url += string.Format("ProcessID={0}",ProcessID);
			return url;
		}
		/// <summary>
		/// Used to redirect to the URL provided and transfer the ProcessContext
		/// to that URL too.
		/// </summary>
		/// <param name="url"></param>
		/// <param name="endRequest"></param>
		public void Redirect(string url, bool endRequest)
		{
			AttachProcessToUrl(url);
			HttpContext.Current.Response.Redirect(url,endRequest);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
New Zealand New Zealand
My name is Alex James, a software architect and developer from Auckland, New Zealand. I have 10 years of business experience working on and managing IT projects. My team and I recently decided to open source Base4.NET: a very exciting tool for creating integrated business applications.

Comments and Discussions