0

here is my table:

table name: data

fields:

  • Id
  • category
  • description
  • imagePath

I'm building a webApi that returns all imagePath or by id.

web api controller:

namespace task.Controllers
{
    public class ImagesController : ApiController
    {
        DataEntry db = new DataEntry();

        public IEnumerable<datum> GetImages()
        {
            var imagePath = from m in db.data select m;
            return imagePath;
        }

        public IHttpActionResult GetImages(int id)
        {
            var imagePath = from m in db.data select m;
            var imagPath = imagePath.Where((p) => p.Id == id);

            if (imagePath == null)
            {
                return NotFound();
            }

            return Ok(imagePath);
        }
    }
}

It's returning all fields (Id,category, description and imagePath) instead of imagePath only.and for the select by Id method it's not working also, so what is wrong??

Jon Susiak
  • 4,948
  • 1
  • 30
  • 38
mohammad
  • 297
  • 1
  • 5
  • 19

1 Answers1

0

For the imagePath, try specifying it in the linq query like so:

public IEnumerable<string> GetImages()
{
    var imagePath = from m in db.data select m.imagePath;
    return imagePath.ToList();
}

For the select by id, try this:

public string GetImages(int id)
{
    var imagePath = from m in db.data select m;
    var imagPath = imagePath.Where((p) => p.Id == id);

    if (imagPath == null)
    {
        return NotFound();
    }
     return Ok(imagPath.imagePath);
}

Double check for typo such as imagPath and imagePath too

Andy Refuerzo
  • 3,312
  • 1
  • 31
  • 38
  • can I retrieve a json file instead of xml?I tried changing the webconfig.cs file like said http://stackoverflow.com/questions/9847564/how-do-i-get-asp-net-web-api-to-return-json-instead-of-xml-using-chrome but gave me a 404 error – mohammad Oct 30 '14 at 12:19
  • have you tried this? http://stackoverflow.com/a/26128440/1846040 from the same link you gave – Andy Refuerzo Oct 30 '14 at 12:23