Click here to Skip to main content
15,886,578 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.Collections;

namespace Base4.Web.UI
{
	/// <summary>
	/// Summary description for ProcessParticipantPage.
	/// </summary>
	public class ProcessParticipantPage: System.Web.UI.Page, IProcessParticipant
	{
		/// <summary>
		/// If the ProcessID is passed to us via the Querystring... attach to that 
		/// ProcessID
		/// </summary>
		protected override void OnInit(EventArgs e)
		{
			ProcessID = Request.QueryString["ProcessID"];
			base.OnInit (e);
		}
		/// <summary>
		/// Since we store the ProcessID in the Viewstate we need to make sure it is available
		/// </summary>
		public override bool EnableViewState
		{
			get{return true;}set{if (value == false) throw new Exception("You can't participate in a process without ViewState being enabled");}
		}

		#region IProcessParticipant Members
		/// <summary>
		/// We use this method to attach a ProcessID to the querystring of a target URL
		/// </summary>
		/// <param name="url">URL to decorate</param>
		/// <returns>decorated URL</returns>
		public string AttachProcessToUrl(string url)
		{
			if (url.IndexOf("?") == -1) 
				url = url + "?";
			else
				url = url + "&";
			return url + string.Format("ProcessID={0}",ProcessID);
		}
		/// <summary>
		/// For clarity we store the ProcessContext in the ViewState...
		/// It is just a GUID so there is almost no overhead
		/// </summary>
		public string ProcessID
		{
			get
			{
				
				if (this.ViewState["ProcessID"] == null)
					this.ViewState["ProcessID"] = Guid.NewGuid().ToString();
				return this.ViewState["ProcessID"] as string;
			}
			set
			{
				if (value == null) return;
				else this.ViewState["ProcessID"] = value;
			}
		}
		/// <summary>
		/// For simplicity lets store the ProcessContext in the Session
		/// However this could be stored just about anywhere...
		/// 
		/// Also for optimum flexibility the ProcessContext is just a Hashtable
		/// </summary>
		public Hashtable ProcessContext
		{
			get
			{
				Hashtable results = Session[ProcessID] as Hashtable;
				if (results == null)
				{
					results = new Hashtable();
					Session[ProcessID] = results;
				}
				return results;
			}
		}

		#endregion
	}
}

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