0

I created an API in Visual Studio Community 2017 that is supposed to load data from a MS SQL server. I created getters for the API to look for certain fields, ex ID, Name. The name getter is working but the ID getter stopped working after my first test and I have no idea why since the other getter are still working.

    public IEnumerable<TableName> GET()
    {

            DBEntities entities = new DBEntities();
            return entities.TableName.ToList();

    }
    public TableName Get(string name)
    {
        DBEntities entities = new DBEntities();
        return entities.TableName.FirstOrDefault(e => e.Name == name);
    }
    public TableName Get(int id)
    {
        DBEntities entities = new DBEntities();
        return entities.TableName.FirstOrDefault(e => e.ID == id);
    }

Does anyone know what I need to do to make the ID getter to work again?

ibubi
  • 2,469
  • 3
  • 29
  • 50
TheJappad
  • 25
  • 5

2 Answers2

0

You can not have two actions with the same name, see here
What you can do is

 [HttpGet, ActionName("GetByName")]
 public TableName Get(string name)
 {
     DBEntities entities = new DBEntities();
     return entities.TableName.FirstOrDefault(e => e.Name == name);
 }
 [HttpGet]
 public TableName Get(int id)
 {
     DBEntities entities = new DBEntities();
     return entities.TableName.FirstOrDefault(e => e.ID == id);
 }

Or you can play with the route configs.

Community
  • 1
  • 1
ibubi
  • 2,469
  • 3
  • 29
  • 50
  • I tried a few different things, including what you said but my ID one gets a null value. Do you know any other workaround for this? – TheJappad Jul 31 '17 at 18:46
0

Thanks for the hint ibubi.

I ended up changing the Get(int id) to GetID(int id) and same for the name and it started doing what I wanted it to.
For example,

From

 public TableName **Get**(int id)
{
  DBEntities entities = new DBEntities();
  return entities.TableName.FirstOrDefault(e => e.ID == id);
}

To

 public TableName **GetID**(int id)
{
  DBEntities entities = new DBEntities();
  return entities.TableName.FirstOrDefault(e => e.ID == id);
}
TheJappad
  • 25
  • 5