ASP.NET MVC 5 DateTime weirdness

  • So I have this MVC action that accepts DateTimes, and in the URL I'm passing them in the format yyyyMMddHHmm, as that's really easy to type and there's no issues with having to escape funny characters. Thing is, MVC doesn't want to deserialise them properly, and the parameters to my action are always null. At the moment I'm manually pulling the DateTimes out of the RouteData, but there's got to be a better way.

    I'm using convention-based routing and the DateTimes need to remain nullable.

  • @RaceProUK Make the action argument a string instead and parse within the method?

  • @skotl I could do that, but it doesn't really seem like a better way. Plus, I'm in the process of adding more routes, and I'd rather they don't interfere with each other too much

  • @RaceProUK Does MVC explicitly support datetimes as controller method arguments? Maybe MSDN has more info. Don't you need to pass a format when you parse a date (in general) or does the parser by default try several formats and takes the first one to parse?

  • I take it that you've double-checked the time zone that the IIS process is running in, and that the format you're using can be parsed in that TZ?

  • @skotl The DateTimes I'm working with are independent of timezone

  • @RaceProUK could you please post your route mappings?

    I've never tried that, but it looks like is a step in doing what you want.

  • @Johan_B Gonna have to wait till Monday I'm afraid; the directors decided to send everyone home early :)

  • @RaceProUK Another thing to consider, is if you are testing code you may be in a situation where Visual Studio is "helpfully" throwing away variables it can tell you do nothing with. I have been in that situation before. Try using the variables for something further down the code, and see if Visual Studio magically brings the values in after that. I kid you not, I have seen that happen ;)

  • I am bit late to the party, but the cleanest way I have found to resolve this DateTime parsing is to use a custom Routehandler.

    The work is here :

    public class RouteConfig
        public static void RegisterRoutes(RouteCollection routes)
                name: "Date"
                , url: "bydate/{controller}/{action}/{date}"
                , defaults: new { controller = "Home", action = "Index", datespecialformat = UrlParameter.Optional }
                , constraints: new { date = @"\d{12}" }).RouteHandler = new DateRouteHandler();
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    public class DateRouteHandler : MvcRouteHandler
        protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
            var dateValue = requestContext.RouteData.Values["date"];
                var dateS = dateValue.ToString();
                requestContext.RouteData.Values["date"] = DateTime.ParseExact(dateS, "yyyyMMddHHmm", CultureInfo.InvariantCulture.DateTimeFormat);
            return base.GetHttpHandler(requestContext);

  • The convention based routing you should avoid IMHO. It causes a whole lot of problems that are solved via attribute routing.

  • @Johan_B

    That is a nice solution. Very much the .NET way on that. I really like that.

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.