I am working on a web API with MVC and C#, I try to return a list of images which are stored in folder and paths of them in the database, I'm trying that:
public class Image {
public int Id { get; set; }
public string Path { get; set; }
public int Item_Id { get; set; }
public bool isMain { get; set; }
}
and in image controller I call this method:
[HttpGet]
[ActionName("GetImageByItemId")]
public HttpResponseMessage GetImages(int id)
{
try
{
using (var ctx = new ApplicationDbContext())
{
var entity = ctx.Images.Where(e => e.Item_Id == id).ToList();
// ctx.Images.FirstOrDefault(e => e.Item_Id == id);
if (entity != null)
{
List<HttpResponseMessage> shapes = new List<HttpResponseMessage>();
HttpResponseMessage response = new HttpResponseMessage();
for (int i = 0; i < entity.Count; i++)
{
String filePath = HostingEnvironment.MapPath("~/Images/" + entity[i].Path + ".jpg");
FileStream fileStream = new FileStream(filePath, FileMode.Open);
response.Content = new StreamContent(fileStream); // this file stream will be closed by lower layers of web api for you once the response is completed.
response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
shapes.Add(response);
}
// return response;
// return Request.CreateResponse(HttpStatusCode.OK, shapes);
return ControllerContext.Request
.CreateResponse(HttpStatusCode.OK, new {shapes});
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "The Images With ID" + id.ToString() + " Not Found");
}
}
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
}
}
if I use
return response;
or
return shapes[1];
it's work to return one image but I need it return a list of image, how to do that?