RESTful API를 만들일이 있어서 ASP.NET MVC로 Json출력하도록 WebApi Controller를 만들었는데
1 2 3 4 5 |
<Message>오류가 발생했습니다.</Message> <ExceptionMessage> 'ObjectContent`1' 유형에서 콘텐츠 형식 'application/xml; charset=utf-8'의 응답 본문을 직렬화하지 못했습니다. </ExceptionMessage> <ExceptionType>System.InvalidOperationException</ExceptionType> |
혹은 영문으로
1 |
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'. |
위와 같은 에러가 났다.
구글신께 여쭤보니 몇가지 해결책이 있었는데 아래의 방법만 성공하였다.
1 2 3 4 5 |
protected void Application_Start() { GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter); ... |
Global.asax파일의 Application_Start()가장 위에 저 2줄을 추가해주면 깔끔하게 해결된다.
참고 : http://stackoverflow.com/questions/12641386/failed-to-serialize-the-response-in-web-api
ASP.NET MVC – RESTful API를 만들때 삽질 기록