0

So, I have 3 methods in my API:

public List<FlatResponse> GetFlats()
public Flat Reserve(int id, int customerId, string service)
public List<FlatResponse> SearchFlats(double budget, double surface)

Now, somehow, for every response the API uses the GetFlats() method.

Perhaps I am using the wrong url?

To reserve flat, I use

myUrl.com/api/flats/?id=1&customerId=2&service=someservice

. To search for specific flats, I use

myUrl.com/api/flats/?budget=500&surface=30

What am I doing wrong?

EDIT:

It is possible that my project isn't structured correctly. Although it works in another API.

My Flatcontroller class

 public class FlatsController : ApiController
    {
        public List<FlatResponse> GetFlats()
        {
            ...

        }

        public Flat Reserve(int id, int customerId, string service)
        {
            ...
        }

        public List<FlatResponse> SearchFlats(double budget, double surface)
        {
           ...
        }
    }

The flatresponse class

public class FlatResponse
    {
        public int Id { get; set; }
        public string Description { get; set; }
        public string Street { get; set; }
        public int HouseNumber { get; set; }
        public int PostalCode { get; set; }
        public string City { get; set; }
        public double RentalPrice { get; set; }
        public double Surface { get; set; }
        public int ContractTime { get; set; }
        public DateTime StartDate { get; set; }
        public List<string> Facilities { get; set; }
        public string ContactPersonName { get; set; }
        public string ContactPersonEmail { get; set; }
        public string ContactPersonTelephone { get; set; }
        public bool Reserved { get; set; }
        public string DetailUrl { get; set; }
        public string ImageUrl { get; set; }
    }
5
  • 1
    Your methods aren't proper controller methods (they don't return any kind of ___Result). Where are the controller methods? Commented Jan 2, 2013 at 17:22
  • Please show the flatsController class Commented Jan 2, 2013 at 17:23
  • 1
    Also, is there supposed to be a difference between URL 1 and URL 2? There must have been a reason you posted each? Commented Jan 2, 2013 at 17:26
  • My mistake, copy & paste without edit :D thanks Commented Jan 2, 2013 at 17:27
  • Is this question about ASP.NET MVC? If so, please add at least the tag asp.net-mvc. Commented Jan 2, 2013 at 17:29

2 Answers 2

3

I think the problem might be that you are not overriding the conventions: GetFlats has "get" as prefix so it is call for the GET the requests.

Try to use the verbs attributes, to that the convention is overriden:

[HttpGet] public List<FlatResponse> GetFlats()
[HttpGet] public Flat Reserve(int id, int customerId, string service)
[HttpGet] public List<FlatResponse> SearchFlats(double budget, double surface)

Regards,

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

Comments

2

From your code, I can relate Flat as an Entity, which has 3 get methods. However from the definition, except the GetFlats method, other methods does not follow the convention to tell MVC API controller that they are get methods. To enable this either follow @Hugo answer to prefix [HttpGet] attribute or prefix the method definitions with get like GetReserve (? may be method names should change).

After doing that,

myUrl.com/api/flats/?id=1&customerId=2&service=someservice

should call GetReserve and

myUrl.com/api/flats/?budget=1&surface=2

should call GetSearchFlats.

1 Comment

Indeed, Hugo was correct. I didn't realize you had to have the prefix 'get'. Only 1 correct answer and Hugo was first, but still thanks man!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.