0

Hi trying here to have multiple diff Get() requests in my API, but I am getting an error

already defines a member called 'Get' with the same parameter types

How should I change this so that I can all of my Get statements

 public IEnumerable<appinfo> Get() { using (XamarinEntities entities = new XamarinEntities()) return entities.appinfoes.ToList(); } public IEnumerable<string> Get() { return new string[] { "id", "fname", "lname", "phone", "company", "approveduser" }; } public appinfo Get(string email) { using (XamarinEntities entities = new XamarinEntities()) return entities.appinfoes.FirstOrDefault(e => e.email == email); } 

1 Answer 1

2

Your methods need to have either unique names or names with a unique signature (i.e. parameters). The easy solution here is to just give your methods unique names that are more descriptive:

[HttpGet] [Route("/entities")] public IEnumerable<appinfo> GetEntities() { using (XamarinEntities entities = new XamarinEntities()) return entities.appinfoes.ToList(); } [HttpGet] [Route("/properties")] public IEnumerable<string> GetProperties() { return new string[] { "id", "fname", "lname", "phone", "company", "approveduser" }; } [HttpGet] [Route("/appinfo")] public appinfo GetAppInfo(string email) { using (XamarinEntities entities = new XamarinEntities()) return entities.appinfoes.FirstOrDefault(e => e.email == email); } 

You can use the Route attribute to define the unique URL to access each method.

Sign up to request clarification or add additional context in comments.

9 Comments

I currently access my 1st Get() like this localhost/api/xamarin/ -- if I rename it to GetEntitities will I still access the same way? Or is that why you are adding in the [Route...] above so you can have multiple methods use the same URL?
You can't have multiple methods use the same URL (unless you used different HTTP verbs like POST, PUT, etc). How would the framework know which method to execute? Ultimately you can name the C# methods whatever you want (I would choose something descriptive) and then using the Route attributes you can define a different URL for each method.
all the code is in my XamarinController so the api hits localhost/api/xamarin - should I create a seperate controller then so that I can have my second Get() request pointing to a different url like localhost/api/secondController
Why don't you just use the Route attributes to define your URL? Using the attributes you can define your routes to be something like localhost/api/xamarin/entities, localhost/api/xamarin/properties and localhost/api/xamarin/appinfo.
If it works on one machine and not the other it's probably a different issue outside the scope of this question. Maybe a difference in the version of ASP.NET MVC on the server? If this is a new app I'd strongly suggest using ASP.NET Core instead and you'll avoid these types of issues.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.