2

Problem:

Given my Asp.NET Web API is set up as follows

Platform: ASP.NET 4.5, Owin, C#,
Globalisation Culture: en-GB

When I create a Url link using UrlHelper in the ApiController with date from set to Today, 2nd Sept 2016:

var someObject = new SomeObject{ ..., DateFrom = DateTime.Today, ... };    
base.Url.Link("whatever", someObject );

Then I get a result where the date is encoded to en-US (09/02/2016 00:00:00)

http://localhost:52321/...DateFrom=09%2F02%2F2016%2000%3A00%3A00

Questions

How do override the process so that the output is more "friendlier", thus

http://localhost:52321/...DateFrom=2016-09-02

How to I achieve this without converting to an anonymous object with a string date, thus

var someObject = new SomeObject{ ..., DateFrom = DateTime.Today, ... };     
var anon = new { ..., DateFrom = someObject.DateFrom.ToString("yyyy-MM-dd"), ...}
base.Url.Link("whatever", anon ); // no thanks

Update

At the very least, I want the encoding to be en-GB (02/09/2016 00:00:00). Because I find the behaviour very odd since my current culture is en-GB and not en-US. Thus...

http://localhost:52321/...DateFrom=02%2F09%2F2016%2000%3A00%3A00
  • Why aren't you just doing : var someObject = new SomeObject{ ..., DateFrom = DateTime.Today.ToString("yyyy-MM-dd"), ... }; ? – Gilles Sep 02 '16 at 13:47
  • you can't I guess until and unless you convert that into string. – Keppy Sep 02 '16 at 13:57
  • HI @Gilles. Because the target endpoint is using a model with [FromUri] binding, and I am using that same model to create the link. Hence, the endpoint looks like `public dynamic Get([FromUri]SomeObject @params){...}`. Why create an anonymous object when I already have the appropriate model? – user2331287 Sep 02 '16 at 13:58
  • @user2331287 Ah, sorry. I just understood that DateFrom is a DateTime and you pass it directly. So what you want is to change how the DateTime object will get converted by the url helper. – Gilles Sep 02 '16 at 14:06

0 Answers0