title: Url.Action MVC3 not recognizing route parameter when building link
body: <p>When adding custom routing constraints to my route parameters I am finding that it is breaking the Url.Action method I use to build my links. If the route constraint is simply a regular expression then the Url.Action method continues to recognize the parameter, however if it is a custom constraint which I define, Url.Action method gives my parameter as a request parameter.</p>
  
  <p>Here is my route definition:</p>
  
  <pre><code>routes.MapRoute(
              "Event",
              "Events/{strDate}",
              new { controller = "Events", action = "Index", strDate = DateTime.Today.ToString("yyyy-MM-dd") },
              new { strDate = new IsValidDateConstraint() },
              new[] { "MyProject.Controllers" }
          );
  
  routes.MapRoute(
              "Default",
              "{controller}/{action}/{id}",
              new { controller = "Home", action = "Index", id = UrlParameter.Optional },
              new[] { "MyProject.Controllers" }
          );
  </code></pre>
  
  <p>The IsValidDateConstraint class inherits from IRouteConstraint and returns true or false if the strDate parameter parses correctly to a DateTime object:</p>
  
  <pre><code>public class IsValidDateConstraint : IRouteConstraint
  {
      public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
      {
          if (routeDirection == RouteDirection.IncomingRequest)
          {
              DateTime dt = new DateTime();
  
              if (DateTime.TryParse(values["strDate"].ToString(), out dt))
                  return true;
          }
  
          return false;
      }
  }
  </code></pre>
  
  <p>Using the Url.Action method to build URL's:</p>
  
  <pre><code>@Url.Action("Index", "Events", new { strDate = ViewBag.CurrentDate.AddDays(1).ToString("yyyy-MM-dd") })
  </code></pre>
  
  <p>The resulting link is: /Events?strDate=2012-08-15</p>
  
  <p>Everything routes correctly if I type in /Events/2012-08-15, it's just that the Url.Action method is not recognizing that strDate is a parameter defined in my route only when I apply my custom routing constraint. If I comment out the custom routing constraint then the Url.Action method maps the URL correctly.</p>
  
  <p>Any ideas on why the Url.Action is not recognizing my route parameter when I have a custom route constraint defined?</p>