Dashboard > MonoRail > Home > Routing tips
Routing tips
Added by Gauthier Segay, last edited by Gauthier Segay on Sep 01, 2008
Labels: 


MonoRail routing service is meant to be extensible.

Here should be random collected tips

Tip: allow identifiers with dot to be matched in url parts

You may want to have some url parts with dot in it such as

/profile/my.user.name/form.mr

Warning

this currently should be considered as a hack and is not embeded in the framework

a test case speaks more than documentation:

RoutingTest.cs
[Test]
public void WithDot() {
	
	PatternRoute route = new MyPatternRoute("/<controller>/<id>/<action>.ext")
	.Restrict("action").AnyOf("index")
	.Restrict("id").WithDot()
	;
	RouteMatch match = new RouteMatch();
	Assert.IsTrue(route.Matches("/some/123/index.ext", CreateGetContext(), match) > 0);
	Assert.AreEqual("some", match.Parameters["controller"]);
	Assert.AreEqual("123", match.Parameters["id"]);
	Assert.AreEqual("index", match.Parameters["action"]);
	Assert.IsTrue(route.Matches("/some/id.with.dot.in.it/index.ext", CreateGetContext(), match) > 0);
	Assert.AreEqual("some", match.Parameters["controller"]);
	Assert.AreEqual("id.with.dot.in.it", match.Parameters["id"]);
	Assert.AreEqual("index", match.Parameters["action"]);
}

[Test]
public void WithoutDot() {
	PatternRoute route = new PatternRoute("/<controller>/<id>/<action>.ext")
		.Restrict("action").AnyOf("index");

	RouteMatch match = new RouteMatch();

	Assert.IsTrue(route.Matches("/some/123/index.ext", CreateGetContext(), match) > 0);
	Assert.AreEqual("some", match.Parameters["controller"]);
	Assert.AreEqual("123", match.Parameters["id"]);
	Assert.AreEqual("index", match.Parameters["action"]);

	Assert.IsFalse(route.Matches("/some/id.with.dot.in.it/index", CreateGetContext(), match) > 0);
}

Implementation of the feature

MyPatternRoute.cs
public class MyPatternRoute : PatternRoute {
	public MyPatternRoute(string pattern) : base(pattern) { }

	
	protected override string[] GetUrlParts(string url) {
		System.Collections.Generic.List<string> splits = new System.Collections.Generic.List<string>(url.Split(new char[] { '/' }, System.StringSplitOptions.RemoveEmptyEntries));
		string last = splits[splits.Count - 1];
		splits.RemoveAt(splits.Count - 1);
		splits.AddRange(last.Split(new char[] { '.' }, System.StringSplitOptions.RemoveEmptyEntries));

		return splits.ToArray();
	}
}

public static class Extensions {
	public static PatternRoute WithDot(this PatternRoute.RestrictionConfigurer configurer) {
		return configurer.ValidRegex(@"[a-zA-Z,_,0-9,-,\.]+");
	}
}

Site running on a free Atlassian Confluence Community License granted to Castle Project. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.4 Build:#809 Jun 12, 2007) - Bug/feature request - Contact Administrators