2016-09-29 10 views
1

私は2つの異なるAPIエンドポイントを持っています。両方のコントローラはApiControllerを継承します。どちらも、単一オブジェクトを返します。それぞれのオブジェクトのフィールドは、DateTimeです。同じAPI内の異なるエンドポイントが異なる日付形式を返すのはなぜですか?

API 1:

public class ReadingProgressResponse 
{ 
    public DateTime DateTime { get; set; } 
    // and other properties 
} 

サンプル応答:

{ 
    "dateTime": "2016-09-28T14:30:26" 
} 

API 2

Response1のように定義される

[Route("OtherThings/{otherThingId:guid}/FirstThing", Name = "GetFirstThing")] 
[HttpGet] 
[ResponseType(typeof(Response1))] 
public IHttpActionResult GetFirstThing(Guid otherThingId, [FromUri] DateTime? modifiedDate = null) 
{ 
    var things = GetThings(otherThingId, modifiedDate); 
    if (!things.Any()) 
    { 
     return NotFound(); 
    } 

    return Ok(new Response1(things.First())); 
} 

public class ScreenSessionResponse 
{ 
    public DateTime ExpirationTime { get; set; } 
    // and other fields 
} 

サンプル応答:API 1日の終わりに "Z" を有するが、API 2ないこと

{ 
    "expirationTime": "2016-09-28T14:48:09Z" 
} 

注意Response2のように定義される

[HttpGet] 
[Route("{someId:guid}", Name = "SomeName")] 
[ResponseType(typeof (Response2))] 
public IHttpActionResult GetResponse2(Guid someId) 
{ 
    var data = GetSomeData(someId); 

    return Ok(new Response2(data)); 
} 

そうです。

なぜですか?レスポンスのフォーマット方法を制御できますか?

答えて

1

はい、日付のフォーマット方法を制御できます。

あなたのGlobal.asaxののOnStartメソッドでこのコードを試してみてください。詳細情報については

// Convert all dates to UTC 
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; 
json.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc; 

、見た目here

を持っています
関連する問題