Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
Proper way to pass date objects between a javascript/typescript frontend and a ASP.net Core 5 backend

I'm developing a web application where the backend is a REST web API written in ASP.net Core 5 and the frontend is an Angular application written in TypeScript.

One of my ASP.net backend APIs returns an instance of this C# object:

public class MyClass
{
  DateTime expires {get; set;}
  string ticket {get; set;}
}

In my Angular app, I call this API with Angular's HTTP Client and deserialize the result as an instance of the following TypeScript class:

export class MyClass
{
  expires: Date;
  ticket: string;
}

However, this doesn't work properly because because if I inspect the TypeScript object once it has been returned I can see that the expires field actually contains a string, not a Date object.

I imagine this happens because the data travels between backend and frontend in JSON format, which doesn't support types but only knows about strings and numbers, so the C# DateTime object is converted to a string.

My question is: what is the best way to handle this problem? I would like to keep my objects strongly typed if possible... is there some configuration I can do on the ASP.net and/or Angular side so that this kind of serialization works automatically? Or if not, what is the next best approach?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

This is the solution I put together from the suggested answers in the comments:

return this.http.get<MyClass>(restUrl, { observe: 'response' })
      .pipe(map(response => {
        if (response.ok) {
          response.body.expires = new Date(response.body.expires);
          this.setLoginResult(response.body);
        }
        return response;
      }));
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda