Search code examples
c#asp.net-coreasp.net-core-webapi

ASP.NET Core 6.0 Web API: Car model


I'm started learning ASP.NET Core, and I am trying to create a web api. I have a demo Car class that I want, in Javascript this is how my Car object looks like:

{
    make: 'AUDI',
    model: 'A3',
    price: 150,
    rentalType: 'day',
    specs: {
        transmission: 'Automatic',
        fuelType: 'Petrol',
        numOfSeats: 5
    },
    imgSrc: 'https://m.atcdn.co.uk/vms/media/0f40778ee8f8402c8cc51fe08eaf2dfd.jpg',
    info: "The Audi A3 is a subcompact executive/small family car (C-segment) manufactured and marketed by the German automaker Audi AG since September 1996, currently in its fourth generation.The first two generations of the Audi A3 were based on the Volkswagen Group A platform, while the third and fourth generations use the Volkswagen Group MQB platform.",
    highlights: {
        "2.0L": 'Turbo 2000CC',
        HORSEPOWER: 200,
        "TOP SPEED": '220 Kmh',
        Gears: 7
    }
},

How should the ASP.NET Core model look like? I'm using ASP.NET 6.0 Core Web API. Thanks


Solution

  • Models

    public class Highlights
    {
            [JsonPropertyName("2.0L")]
            public string _20L { get; set; }
            public int HORSEPOWER { get; set; }
    
            [JsonPropertyName("TOP SPEED")]
            public string TOPSPEED { get; set; }
            public int Gears { get; set; }
    }
    
    public class Specs
    {
            public string transmission { get; set; }
            public string fuelType { get; set; }
            public int numOfSeats { get; set; }
    }
    
    public class Root
    {
            public string make { get; set; }
            public string model { get; set; }
            public int price { get; set; }
            public string rentalType { get; set; }
            public Specs specs { get; set; }
            public string imgSrc { get; set; }
            public string info { get; set; }
            public Highlights highlights { get; set; }
    }
    

    API:

    [HttpGet]
    public IActionResult Car()
    {
        Root root = new Root()
                {
                    make = "AUDI",
                    model = "A3",
                    price = 150,
                    rentalType = "day",
                    specs = new Specs()
                    {
                        transmission = "Automatic",
                        fuelType = "Petrol",
                        numOfSeats = 5
                    },
                    imgSrc = "https://m.atcdn.co.uk/vms/media/0f40778ee8f8402c8cc51fe08eaf2dfd.jpg",
                    info = "The Audi A3 is a subcompact executive/small family car (C-segment) manufactured and marketed by the German automaker Audi AG since September 1996, currently in its fourth generation.The first two generations of the Audi A3 were based on the Volkswagen Group A platform, while the third and fourth generations use the Volkswagen Group MQB platform.",
                    highlights = new Highlights()
                    {
                        _20L = "Turbo 2000CC",
                        HORSEPOWER = 200,
                        TOPSPEED = "220 Kmh",
                        Gears = 7
                    }
                };
    
        return Ok(root);
    }
    

    enter image description here