3
I have a request that’s being made this way at angle 5.
enviarEmail(titulo: TitulosCobranca, unidade: UnidadeEmpresa) {
let param: any = {
titulo: titulo,
unidade: unidade
}
return this.http.post(this.UrlService + '/TitulosCobranca/envio', param)
.map((res: any) => res.data);
}
In C# I am trying to receive the data as follows:
[HttpPost]
[Route(*_minhaRota_*)]
public async Task<IActionResult> EnviarEmail([FromBody] ParametrosPesquisaViewModel param)
{
UnidadesEmpresaViewModel unidadeEmpresa = param.unidade;
TitulosCobrancaViewModel titulo = param.titulo;
/* Continue ... */
When I put the breakpoint and I’ll find out what’s going on the value of param
is null.
Putting the console.log
before the request the data is there.
Below print of the request body:
public class ParametrosPesquisaViewModel
{
public int Index { get; set; } = 1;
public string Search { get; set; } = "";
public string Order { get; set; } = "";
public string razaoSocial { get; set; }
public string cnpj { get; set; }
public string telefone { get; set; }
public int pagina { get; set; }
public int nrRegistros { get; set; }
public ParamConsultaViewModel Param { get; set; }
public UnidadesEmpresaViewModel unidade { get; set; }
public TitulosCobrancaViewModel titulo { get; set; }
}
Above is my Model.
Someone has a light to give me?
A hint... in the browser developer tools, has a session to see the data that is sent. In Chrome is the network tab. Check the body of the requisition there - and if possible, edit the post to include the body of the request, as it appears in this tab. If the information is missing in the body, the problem is in Angular. If it is present, the problem is in C#.
– Oralista de Sistemas
Yes, the data is present Renan. I had already checked this too. I added a print with the body of the request.
– Lucas Brogni
I never worked with Angular... When making the request "on the nail" with Ajax, if I informed the type of the request (contenttype) as application/json, the information would only arrive on the . NET side if the body was valid JSON. I see that the body of your request is not JSON, so I suggest, as a test, to do something like this: Return
this.http.post(this.UrlService + '/TitulosCobranca/envio', JSON.stringify(param)).map((res: any) => res.data);
– Oralista de Sistemas
The request is as JSON already Renan. I ended up cutting the image but the request type is application/json and in the preview I went back it this way. But the data is as JSON as suggested.
– Lucas Brogni
And his
ParametrosPesquisaViewModel
has aUnidadesEmpresaViewModel unidade
and aTitulosCobrancaViewModel titulo
?– Leandro Angelo
Show us your route, on the controller, I believe the problem is the route.
– Rodrigo K.B
@Leandroangelo I added my model for you to view.
– Lucas Brogni
@Rodrigok. B my controller is there
[HttpPost]
 [Route("/api/TitulosCobranca/envio/")]
public async Task<IActionResult> EnviarEmail([FromBody] ParametrosPesquisaViewModel param)
{
 UnidadesEmpresaViewModel unidadeEmpresa = param.unidade;
 TitulosCobrancaViewModel titulo = param.titulo;
The error pops right when I try to access param.drive, so I didn’t add what’s underneath it.– Lucas Brogni
@Rodrigok. B my request does not have the /api because I already have the variable declared in my super class....
protected UrlService: string = "http://localhost:57512/api/";
– Lucas Brogni
ok, apparently the problem is that the object stops, when you pass it in the post, it has not been converted to json. I believe that if you convert, it will work.
– Rodrigo K.B
I don’t know how it works at angular5 for this type of conversion, but I saw an example at angular2 of the staff using
JSON.stringify(product)
, which is what I use in Angularjs– Rodrigo K.B
Let’s go continue this discussion in chat.
– Lucas Brogni