2
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 it this way, but I can’t access it:
Webapiconfig Route:
config.Routes.MapHttpRoute("DefaultApiWithActionAndSkip",
"api/{controller}/{action}/{skip}",
defaults: new { skip = RouteParameter.Optional });
Place 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"));
}
Where I get the response sent by the Web API (I know it works because in this place where I do the request I have several other requests being made (but all with normal variables) and they all work):
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 cannot be null. Parameter name: Entity"
I’m not familiar with c# but in java if you sent a certain content-type , in your case
application/json
should not you "note" the method that is receiving this content-type? In your case theRegistrarTerminal
– Jorge Campos
@Jorgecampos no, it’s not necessary.
– Luiz Negrini
Have you tried a requisition with a payload in handmade JSON using this?
– Leonel Sanches da Silva
@Ciganomorrisonmendez vc says to pass a string as parameter that would be JSON? If it is, yes.
– Luiz Negrini
Another question: are you deserializing an XML or a JSON? It’s not clear to me.
– Leonel Sanches da Silva
@Ciganomorrisonmendez basically xml. was trying with json as well but I couldn’t get it to work on either. I now saw a possible problem, where I mount Stringcontent put 2 parameters in the constructor, encondig.utf-8 and also a content-type application/xml. Changed the error, now says "Value cannot be null. Parameter name: Entity"
– Luiz Negrini
Check your route on
WebApiConfig.cs
. Another thing, theterminal
is coming null?– Uilque Messias
config.Routes.Maphttproute("Defaultapiwithactionandskip", "api/{controller}/{action}/{Skip}", defaults: new { Skip = Routeparameter.Optional }); Terminal is not null.
– Luiz Negrini
insert route up there.
– Luiz Negrini