Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to return an object with its functions from API?

Considering following C# class in .NET

public class Person
{
    public string name { get; set; }
    public string surname { get; set; }
    
    public string Fullname()
    {
        return this.name + " " + this.surname;
    }
}

and the endpoint

[HttpGet("getperson/")]
public ActionResult<Person> getPerson()
{
    ...
    return person;
}

In my Angular app, I have the function

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

getPerson(){
    this.http.get(this.api + "getPerson").subscribe(person => {
        person.fullname() // is it possible to do this if the function is not implemented in the frontend?
    }
}

Could I somehow also pass the function with the object to use it without implementing it in the frontend?

>Solution :

Not possible. You need to write FullName method in the Typescript model.

class Person
{
    name: string;
    surname: string;
    
    constructor(name: string, surname: string){
        this.name = name;
        this.surname = surname;
    }

    fullName(): string {
        return this.name + " " + this.surname; 
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading