get json data and images get json data and images json json

get json data and images


You can create a view model to encapsulate all the desired information. Include a url to where to get the image along with the other metadata

for example.

public class CarModel {  public string markName { get; set; }  public string modelName { get; set; }  public int year { get; set; }  public int id { get; set; }  public string photo { get; set; }}

You would then include a link to the photo action

[HttpGet("list")]public IActionResult List() {    var cars = context.Cars.AsEnumerable();    var models = cars.Select(car =>                             new CarModel {                                markName = car.markName,                                modelName = car.modelName,                                 year = car.year,                                id = car.id,                                photo = Url.RouteUrl("CarPhoto", new { id = car.id })                            }).ToList();    return Ok(models);}[HttpGet("{id}/photo", Name = "CarPhoto")]public IActionResult GetPhoto(int id) {    string path = "blalblabla"    Byte[] b = System.IO.File.ReadAllBytes(path);             return File(b, "image/jpeg");}

An example response could look like

[{   "markName": "Chevrolet",   "modelName": "Spark EV",   "year": 2014,   "id": 1,   "photo": "cars/1/photo"},{   "markName": "Chevrolet",   "modelName": "Volt",   "year": 2014,   "id": 2   "photo": "cars/2/photo"}]