Reputation: 337
I have created web api using Entity Frame work in C#.net. I have a controller class in which i have defined some methods like this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Test_Net_Test_Info.Models;
namespace Test_Net_Test_Info.Controllers
{
public class InfosController : ApiController
{
public List<Info> Get()
{
return InfoRepository.GetAllInfos();
}
public Info Get(int id)
{
return InfoyRepository.GetInfoById(id);
}
public Info Get(string company)
{
return InfoRepository.GetInfoByCompany(company);
}
public Info Get(string contact)
{
return InfoRepository.GetInfoByContact(contact);
}
my InfoRepository class looks like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Test_Net_Test_Info.Models
{
public class InfoRepository
{
public static List<Info> GetAllInfoss()
{
Test_NETEntities dataContext = new Test_NETEntities();
var query = from info in dataContext.Infoss select info;
return query.ToList();
}
public static Info GetInfoById(int id)
{
test_NETEntities dataContext = new test_NETEntities();
v*emphasized text*ar query = (from info in dataContext.Infos where info.ID ==id select info).SingleOrDefault();
return query;
}
public static Info GetInfoByContact(string contact)
{
Test_NETEntities dataContext = new Test_NETEntities();
// var query = (from info in dataContext.Infos where info.Contact == contact select info).SingleOrDefault();
// return query;
//}
public static Info GetInfoByCompany(string company)
{
Test_NETEntities dataContext = new test_NETEntities();
var query = (from info in dataContext.Infos where info.Company == company select info).SingleOrDefault();
return query;
}
I am getting an error Test_Net_Test_Info.Controllers. already defines a member called 'Get' with the same parameter types. I am trying to retrieve data by Company name and Contact name. Kindly help me.
Upvotes: 0
Views: 2213
Reputation: 218847
This is one area where HTTP verbs and C# method signatures don't always get along. One approach could be to combine them into a single method:
public Info Get(string company = null, string contact = null)
{
if (company != null)
return InfoRepository.GetInfoByCompany(company);
if (contact != null)
return InfoRepository.GetInfoByContact(contact);
// throw an exception? some other default action?
}
Then you'd just be relying on the model binder to populate the method arguments based on the request.
(You might also use checks on string.IsNullOrWhiteSpace()
instead of null
.)
Upvotes: 1
Reputation: 156978
Your method signatures are ambiguous. Changing the name will fix the issue:
public Info GetByCompany(string company)
{
return InfoRepository.GetInfoByCompany(company);
}
public Info GetByContact(string contact)
{
return InfoRepository.GetInfoByContact(contact);
}
In both your method calls, the only thing the compiler sees is this (the signature):
Info GetByContact(string)
It doesn't know how to choose between the company
and contact
parameter.
Upvotes: 3