비어 있습니다 .net 나머지 api는 XML
만을 허용합니다. 컨트롤러 메서드가 직접 직렬화하려는 클래스를 반환하면 문제가 없습니다. 하지만 모든 컨트롤러 메서드가 HttpResponseMessage
을 반환하도록 응답 래퍼를 사용하고 싶습니다.HttpResponseMessage가 내용을 serialize 할 수 없습니다.
그래서이 작동합니다 FooController.cs :
public FooResponse PostFoo(FooRequest foo)
{
var fooVal = fooBll.GetFoo();
return new FooResponse { Foo = fooVal;};
}
그러나이되지 않습니다 FooController.cs :
public HttpResponseMessage PostFoo(FooRequest foo)
{
var fooVal = fooBll.GetFoo();
HttpResponseMesage response;
response = fooBll.GetSuccessResponse(Url, new FooResponse {Foo = foovVal;});
return response ;
}
ResponseBLL.cs가 :
public HttpResponseMessage GetSuccessResponse(UrlHelper Url, object obj)
{
this.requestMsg = HttpContext.Current.Items["MS_HttpRequestMessage"] as HttpRequestMessage;
this.responseMsg = this.requestMsg.CreateResponse(HttpStatusCode.OK, obj);
this.responseMsg.Headers.Location = HttpContext.Current.Request.Url;
return this.responseMsg;
}
FooBLL 상속 ResponseBLL, 그래서 전화 할 수 있습니다 fooBll.GetSuccessResponse
. WebApiConfig.cs에서
나는이 : 내가 얻을
config.Formatters.XmlFormatter.UseXmlSerializer = true;
config.Formatters.Remove(config.Formatters.JsonFormatter);
오류 유형의 FooResponse이 예상되지
입니다. 정적으로 알려지지 않은 형식을 지정하려면 XmlInclude 또는 SoapInclude 특성을 사용하십시오.
이제는이 문제에 대해 많은 질문을하고 있으며 저를 신뢰합니다. 나는 성공하지 못했지만 모두 통과했습니다. 내가 생각할 수있는 모든 가능한 조합에서 [DataContract], [DataMember], [Serializable]
과 [XmlInclude]
속성을 시도했다. FooBLL 클래스에 [XmlInclude(typeof(FooResponse))]
, ResponseBLL에 [XmlInclude(typeof(FooBLL))]
등이 있습니다.
도와 주시면 감사하겠습니다.
액션 필터 –
@AlekseyL을 사용하여 농축 왕을하는 것이 훨씬 더 깨끗합니다. 고마워, 나는 그것에 대해 조사 할 것이다. 기본적으로이 래퍼가 다른 방식의 컨텐트를 반환 할 수있는 방법 이었기 때문에이 래퍼를 원했던 이유는 무엇입니까? 'GetFailResponse' 메쏘드도 있습니다.이 메쏘드는 내용 대신에 오류에 대한 정보를 반환합니다. 그것 없이는 내가 볼 수있는 한 그것을 할 수 없다. 나는'FooResponse'와 다른 것을 돌려줘야한다. – John
예외 필터 또는 예외 처리기로 처리 할 수 있습니다 –