I need to pass a parameter to my Controller in the Web API which is a complex object, this object has some normal properties (the most complex is a DateTime). I'm doing the following, but I can not access:
WebApiConfig Route:
config.Routes.MapHttpRoute("DefaultApiWithActionAndSkip",
"api/{controller}/{action}/{skip}",
defaults: new { skip = RouteParameter.Optional });
Location where I make the request:
private CargaInicialHelper()
{
_client = new HttpClient();
_client.DefaultRequestHeaders.Clear();
_client.DefaultRequestHeaders.Accept.Add(new Windows.Web.Http.Headers.HttpMediaTypeWithQualityHeaderValue("application/xml"));
}
_client.DefaultRequestHeaders.Accept.Add(new Windows.Web.Http.Headers.HttpMediaTypeWithQualityHeaderValue("application/json"));
}
ApiController:
public async Task<bool> RegistrarTerminal(Terminal terminal)
{
return await ObterRespostaServicoAsync<bool>("RegistrarTerminal",
new HttpStringContent(JsonConvert.SerializeObject(terminal),
Windows.Storage.Streams.UnicodeEncoding.Utf8,
"application/xml"));
}
private async Task<T> ObterRespostaServicoAsync<T>(string relativePath, HttpStringContent content = null)
{
try
{
var request = new HttpRequestMessage();
if (content == null)
{
request = new HttpRequestMessage(HttpMethod.Get, new Uri(string.Format(URL, relativePath ?? String.Empty)));
}
else
{
request = new HttpRequestMessage(HttpMethod.Post, new Uri(string.Format(URL, relativePath ?? String.Empty)));
request.Content = content;
var teste = await _client.PostAsync(request.RequestUri, content);
}
request.Headers.TryAppendWithoutValidation("Content-Type", "application/xml");
var response = await _client.GetAsync(request.RequestUri);
response.EnsureSuccessStatusCode();
string xml = Utils.RemoveAllXmlNamespaces(await response.Content.ReadAsStringAsync());
reader = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(xml)));
XmlSerializer serializer = new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(reader);
}
catch (Exception e)
{
return default(T);
}
}
Error:
Bad request (500). "Value can not be null. Parameter name: entity "