2017-05-08 89 views
0

我有一個JSON字符串。當我通過POST方法將視圖中的字符串發送到我的控制器時,此JSON的一個屬性是datetime,它以「dd/mm/yyyy」格式映射。但是,當我通過GET方法發送相同的JSON字符串時,相同的屬性映射格式爲「mm/dd/yyyy」,對於天數大於12的日期,該屬性保留爲NULL。如何解決此問題?這可能是Visual Studio問題嗎?我正在使用巴西的日期格式。DateTime格式對於POST和GET方法是不同的

+0

?你能分享一些代碼嗎? –

+0

如果你使用json.net,看看這個:https://stackoverflow.com/questions/18635599/specifying-a-custom-datetime-format-when-serializing-with-json-net – user1845593

回答

0

你需要具體文化上的獲取請求。因爲瀏覽器使用不同的文化比你的服務器,他們應該匹配,

DateTime dt = DateTime.Now; 
    // Sets the CurrentCulture property to U.S. English or whatever your browser using . 
    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); 
    // Displays dt, formatted using the ShortDatePattern 
    // and the CurrentThread.CurrentCulture. 
    Console.WriteLine(dt.ToString("d")); 

更多的檢查:你用哪個庫序列化JSON格式的對象https://msdn.microsoft.com/en-us/library/5hh873ya(v=vs.90).aspx