6

does ASP.NET MVC contain any route contraints baked into the code? if so, how do i define a date-time constraint?

eg. url:

http://mydomain.com/{versionDate}/{controller}/{action}
http://mydomain.com/2010-01-20/search/posts

cheers :)

Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
  • What exactly do you mean by a date-time constraint? Where would the value for that constraint come from? How do you want it to route based on that constraint? Could that constraint simply be a parameter to a controller, which further redirects or calls other controller methods algorithmically? – jrista Mar 02 '10 at 03:59
  • notice how i have a slot in the route for VersionDate? i was hoping that if a person put an invalid date in there, it would error. As such, I thought that it would be best to place a route - constraint on that route parameter .. to prevent bad data getting passed in. – Pure.Krome Mar 02 '10 at 04:05

3 Answers3

12

I ended up making my own route constraint. only took a few mins.

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

namespace Whatever.Your.Funky.Cold.Medina.Namespace.Is
{
    public class DateTimeRouteConstraint : IRouteConstraint
    {
        #region IRouteConstraint Members

        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values,
                          RouteDirection routeDirection)
        {
            DateTime dateTime;

            return DateTime.TryParse(values[parameterName] as string, out dateTime);
        }

        #endregion
    }
}

simple :P

Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
2

You could also set up a constraint on the route, something like so. The regular expression used is not very robust, so you should refine it.

routes.MapRoute( 
    "Version", "
    {versionDate}/{controller}/{action}", 
    new {controller="Search", action="Posts"}, 
    new {versionDate= @"\d\d\d\d-\d\d-\d\d" } 
    ); 

Information from here.

Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
37Stars
  • 2,489
  • 20
  • 23
  • i thought of using a regex, initally, but like you suggested, it's not very robust. which is why i wanted to utilise the power to `DateTime.TryParse(...)`. – Pure.Krome Mar 02 '10 at 21:01
  • 4
    @37Start: don't you think a regular expression like `"\d{4}-\d{2}-\d{2}"` would be more readable and more standard? ;) Or maybe even writing one that would only let in correct dates (month not greater than 12 etc.) – Robert Koritnik Mar 15 '10 at 16:13
  • +1 Absolutely that would be better. I always have to break out the RegEx book when I get around to writing these as I do it infrequently. I wrote that response as I was heading out the door, hence the "you should refine it" comment. – 37Stars Mar 16 '10 at 16:32
0

all of the framework is overide-able so it's possible, with a great deal of pain, to overide the default behaviour of the route engine but i agree with @jrista in that you might want to make it a parameter of the controller else mvc will expect to find /search/posts within the 2010-01-20 folder

griegs
  • 22,624
  • 33
  • 128
  • 205
  • within the 2010-01-20 folder? there are no folders. it's just controllers and their views. Also, it's not part of the action method. I'm actually capturing this in the abstract controller - because all routes will have this. That way, it's KISS. – Pure.Krome Mar 02 '10 at 04:10
  • Hmmm, then you may want to download the source for the framework and see if you can either extend it or find out how to overide the default routing behaviour. – griegs Mar 02 '10 at 04:11
  • I've ended up making a custom route constraint. took me a few mins to do. solved. – Pure.Krome Mar 02 '10 at 04:41