Passing DateTimeOffset as WebAPI query string

前端 未结 8 645
感动是毒
感动是毒 2020-12-11 15:49

I\'ve got a WebAPI action that looks like so:

[Route(\"api/values/{id}\")]
public async Task Delete(string id, DateTimeOffset date         


        
8条回答
  •  孤街浪徒
    2020-12-11 16:30

    The current accepted answer throws away the time zone information, which in some cases is important. The following maintains the time zone and doesn't lose any precision. It also keeps your code succinct when building a query string.

    public static string UrlEncode(this DateTimeOffset dateTimeOffset)
    {
         return HttpUtility.UrlEncode(dateTimeOffset.ToString("o"));
    }
    

提交回复
热议问题