Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

126
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda