0

Good day!

I pass DateTime value as route parameter and it became GET parameter in form like this:

http://example.com/?DateFrom=01%2F01%2F2011%2000%3A00%3A00&DateTo=01%2F31%2F2011%2000%3A00%3A00

For readability: this is an URL encoded from:

http://example.com/?DateFrom=01/01/2011 00:00:00&DateTo=01/31/2011 00:00:00

Is there any way to customize this serialization without using custom routes?

Thanks in advance!

artvolk
  • 9,448
  • 11
  • 56
  • 85

1 Answers1

1

Instead of passing a DateTime you could pass a formatted string:

<%= Html.ActionLink(
    "link text", 
    "someaction", 
    new { 
        DateFrom = Model.DateFrom.ToString("yyyy-MM-dd"),
        DateTo = Model.DateTo.ToString("yyyy-MM-dd"),
    }
) %>
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • The problem is that I pass to route values not single `DateTime` value, but object `UrlParams` which contains about 10 params (filter values). I have an action which accepts this object as a parameter. It works without any manual intervention, but `DateTime` values in URL are ugly... – artvolk Jan 21 '11 at 15:11
  • @artvolk, in this case instead of passing a `UrlParams` object pass a `UrlParamsViewModel` object which will have the dates properly formatted. [AutoMapper](http://automapper.codeplex.com/) can help with the conversion between objects. – Darin Dimitrov Jan 21 '11 at 15:13
  • My `UrlParams` exists only to pass params in URL, so it's a kind of ViewModel already :). It seems I should implement some logic to format\unformat values by hand? – artvolk Jan 21 '11 at 15:14
  • @artvolk, in this UrlParams object you could replace the DateTime properties with strings and this way you could have the proper formats. As an alternative keep the original DateTime as private backing fields and expose corresponding public string properties which will be properly formatted. – Darin Dimitrov Jan 21 '11 at 15:15
  • Yep, it seems I should do this. But what about replacing\customizing query string serialization? Is this even possible? – artvolk Jan 21 '11 at 15:16