Request arrives null on C#

Asked

Viewed 496 times

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:

inserir a descrição da imagem aqui

 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#.

  • Yes, the data is present Renan. I had already checked this too. I added a print with the body of the request.

  • 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);

  • 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.

  • And his ParametrosPesquisaViewModel has a UnidadesEmpresaViewModel unidade and a TitulosCobrancaViewModel titulo?

  • Show us your route, on the controller, I believe the problem is the route.

  • @Leandroangelo I added my model for you to view.

  • @Rodrigok. B my controller is there [HttpPost]&#xA; [Route("/api/TitulosCobranca/envio/")]&#xA;public async Task<IActionResult> EnviarEmail([FromBody] ParametrosPesquisaViewModel param)&#xA;{&#xA; UnidadesEmpresaViewModel unidadeEmpresa = param.unidade;&#xA; TitulosCobrancaViewModel titulo = param.titulo; The error pops right when I try to access param.drive, so I didn’t add what’s underneath it.

  • @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/";

  • 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.

  • 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

Show 7 more comments

1 answer

1


I was able to solve the problem by making the request with an object of my own by mounting it in the request only with the fields that were of my interest, capture in the back-end, in case without passing the complete object.

enviarEmail(titulo: TitulosCobranca, unidade: UnidadeEmpresa) {
    let param: Object = {
        "Titulo": {
            id: titulo.id
        }, 
        "Unidade": {
            id: unidade.id,
            email: unidade.Email
        } 
    }

    return this.http.post(this.UrlService + 'TitulosCobranca/envio', param)
        .map((res: any) => res.data);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.