Since a few days I am trying to figure out how to return from my web api generic response - a wrapper class where one property will have dynamically pointed type.
Below code fragment shows what I want to achieve:
[RoutePrefix("api")]
public class TestController : ApiController
{
[HttpGet]
[Route("test")]
public HttpResponseMessage Test3()
{
Smth smth = new Smth()
{
Something = "dsfdsfdsfs"
};
object apiResponse = this.GetResponse(true, smth);
return base.Request.CreateResponse(HttpStatusCode.OK, apiResponse);
}
public object GetResponse(bool isSuccess, dynamic responseObject, string[] messages = null)
{
return new
{
is_success = isSuccess,
response_object = responseObject,
messages = messages
};
}
}
Unfortunately this apporach does not work - I still get:
ExceptionMessage
Cannot perform serailization of type <>f__AnonymousType0`3[System.Boolean,System.Object,System.String[]] ...
ExceptionType
System.Runtime.Serialization.InvalidDataContractException
StackTrace
in
System.Runtime.Serialization.DataContract.DataContractCriticalHelper.ThrowInvalidDataContractException(String message, Type type) w System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id, RuntimeTypeHandle typeHandle, Type type) w System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type) w System.Runtime.Serialization.DataContractSerializer.GetDataContract(DataContract declaredTypeContract, Type declaredType, Type objectType) w System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) w System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) w System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) w System.Runtime.Serialization.DataContractSerializer.WriteObject(XmlWriter writer, Object graph) w System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content) w System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken) --- Koniec śladu stosu z poprzedniej lokalizacji, w której wystąpił wyjątek --- w System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) w System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) w System.Net.Http.HttpContent.d__49.MoveNext() --- Koniec śladu stosu z poprzedniej lokalizacji, w której wystąpił wyjątek --- w System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) w System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) w System.Web.Http.Owin.HttpMessageHandlerAdapter.d__13.MoveNext()
During my research I found on forum some examples which work well:
[RoutePrefix("api")]
public class TestController : ApiController
{
[HttpGet]
[Route("test")]
public HttpResponseMessage Test3()
{
Smth smth = new Smth()
{
Something = "dsfdsfdsfs"
};
var apiReponse = new
{
is_success = true,
response_object = smth,
messages = new string[] { "dsfsfdds" }
};
return base.Request.CreateResponse(HttpStatusCode.OK, apiReponse);
}
}
Above example works and returns correctly formatted data, but such approach will cause bugs related to naming (in this way I have to specify response structure each time I return it).
From my point of view there is no difference in this two approaches except fact that in first case we get anonymous type and in second case we work with object.
So the question is:
Is it possible to make my first approach work?