Login

Username

Password





Register   Reset password

This is a read-only archive of the forum.

# jmaynor 5/6/2005 6:50 AM
As I prepare to write my first module I want to make certain I understand the members of the ModuleBase and the overall 'deal' with modules.

Could you please give me the lowdown on the URL rewriting and how that is used to send information to a module? Could you also briefly explain how the module 'decides' what UserControl to inject into the page and a word on the UserControls ability to leverage ViewState and events.

Thanks,

Jeremy
# martijnb 5/6/2005 10:09 AM (edited 5/6/2005 10:11 AM)
jmaynorCould you please give me the lowdown on the URL rewriting and how that is used to send information to a module?

The url rewriter doesn't alter the querystring or pathinfo. So both are available to modules. By default, Cuyahoga uses pathinfo parameters for its modules, but nothing stops you from using querystrings.

As an example, the ArticleModule uses the pathinfo parameters to determine which article or category to show:

http://www.martijnboland.com/cuyahoga/10/section.aspx/25
http://www.martijnboland.com/cuyahoga/10/section.aspx/category/1

The ModuleBase class has a virtual method ParsePathInfo() that does nothing more than putting the pathinfo parameters in an array. The ArticleModule class overrides this method to translate the pathinfo into member variables:
protected override void ParsePathInfo()
{
	if (base.ModulePathInfo != null)
	{
		// try to find an articleId

		string expression = @"^\/(\d+)";
		Regex articleIdRegEx = new Regex(expression, RegexOptions.Singleline|RegexOptions.CultureInvariant|RegexOptions.Compiled);
		if (articleIdRegEx.IsMatch(base.ModulePathInfo))
		{					
			this._currentArticleId = Int32.Parse(articleIdRegEx.Match(base.ModulePathInfo).Groups[1].Value);
		}
				
		// try to find a categoryid
		expression = @"^\/category\/(\d+)";
		Regex categoryRegex = new Regex(expression, RegexOptions.Singleline|RegexOptions.CultureInvariant|RegexOptions.Compiled);
		if (categoryRegex.IsMatch(base.ModulePathInfo))
		{
			this._currentCategoryId = Int32.Parse(categoryRegex.Match(base.ModulePathInfo).Groups[1].Value);
		}
	}
}
Could you also briefly explain how the module 'decides' what UserControl to inject into the page and a word on the UserControls ability to leverage ViewState and events.

It's not really a module, but in the ~/Profile.aspx page you can see how to achieve this (in a module you have to do this in a User Control of course). Here, the pathinfo is parsed and the first parameter is the command that indicates which User Control to load (view, edit, register, reset).
If the User Controls are loaded in the Init stage and also after postbacks, ViewState and events work as usual.
private void InitPage()
{
	// Load the right user control based on the pathinfo.
	string pathInfo = HttpContext.Current.Request.PathInfo;
	string[] parameters = Util.UrlHelper.GetParamsFromPathInfo(pathInfo);
	if (parameters != null)
	{
		Control ctrl = null;
		string cmd = parameters[0];
		switch (cmd)
		{
			case "view":
				ctrl = this.LoadControl("~/Controls/ViewProfile.ascx");
				base.Title = "View profile";
				if (parameters.Length == 2)
				{
					// The second pathinfo parameter should be the UserId.
					int userId = Int32.Parse(parameters[1]);
					(ctrl as Cuyahoga.Web.Controls.ViewProfile).UserId = userId;
				}
				break;
			case "edit":
				ctrl = this.LoadControl("~/Controls/EditProfile.ascx");
				base.Title = "Edit profile";
				break;
			case "register":
				ctrl = this.LoadControl("~/Controls/Register.ascx");
				base.Title = "Register";
				break;
			case "reset":
				ctrl = this.LoadControl("~/Controls/ResetPassword.ascx");
				base.Title = "Reset password";
				break;
			default:
				throw new Exception("Invalid command found in pathinfo.");
		}

		this.pnlControl.Controls.Add(ctrl);
	}
}