first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

ASP.NET MVC: “Custom Route Constraints”; James Welch

Here’s the resulting constraint class:

using System; using System.Web; using System.Web.Routing;

public class NotEqual : IRouteConstraint { private string[] _matches = null;

public NotEqual(params string[] matches) { _matches = matches; }

public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { bool foundMatch = false; foreach (string match in _matches) if (String.Compare(values[parameterName].ToString(), match, true) == 0) foundMatch = true;

  return !foundMatch;//not matching!

} }

This was a pretty simple fix, just changing the code to use params string[] instead of a single string argument. You can also use this in replace of Stephen’s code, since you can also pass in a single argument.

[http://jamesewelch.wordpress.com/ 2008/08/11/custom-route-constraints-aspnet-mvc/]

mod date: 2009-09-16T00:15:40.000Z