2014-10-21 3 views
17

Owin을 사용하여 파일 요청과 웹 API를 모두 지원하는 자체 호스팅 서버를 구축하고 있습니다. 그러나 웹 API 요청의 출력은 항상 xml 형식입니다. json에서 출력 할 owin을 어떻게 구성 할 수 있습니까?Owin 자체 호스트 지원 Json 출력을 만드는 방법은 무엇입니까?

코드는 다음과 같습니다 :

class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     app.UseFileServer(new FileServerOptions() 
     { 
      RequestPath = PathString.Empty, 
      FileSystem = new PhysicalFileSystem(@".\files") 
     }); 

     // set the default page 
     app.UseWelcomePage(@"/index.html"); 

     HttpConfiguration config = new HttpConfiguration(); 

     config.Routes.MapHttpRoute 
     (
      name: "DefaultApi", 
      routeTemplate: "api/{controller}/{id}", 
      defaults: new { id = RouteParameter.Optional } 
     ); 

     app.UseWebApi(config); 
    } 
} 
+0

XML로 결과를 요청하면 어떤 헤더를 보내고 있습니까? JSON을 지정하는'Accept' 헤더를 보내고 있습니까? – David

+0

헤더는 아래와 같습니다 : Accept : text/html, application/xhtml + xml, application/xml, q = 0.9, image/webp, */*; q = 0.8 –

답변

28

나는 나 자신에 대답을 발견했다. 모두가해야 할 아래로 JSON 포매터를 추가하는 것입니다

config.Formatters.Clear(); 
config.Formatters.Add(new JsonMediaTypeFormatter()); 
config.Formatters.JsonFormatter.SerializerSettings = 
new JsonSerializerSettings 
{ 
    ContractResolver = new CamelCasePropertyNamesContractResolver() 
}; 

만약 문자열 열거가 설정 StringEnumConverter를 추가 변환해야합니다.

config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new StringEnumConverter()); 
관련 문제