Nate
Nate

Reputation: 847

Issue posting parameter in MVC API

I have a Web API on a site. If the api call does not have a parameter in it, then I can hit it, but if it has a parameter on it, then I cannot hit it. Below is an example of what I am talking about.

[HttpGet]
public string GetFacilityName2()
{
    return "Good";
}

[HttpGet]
public string GetFacilityName(string projectNumber)
{
    return "Never Get Here";
}

My config route is the default at this time.

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

I am using the following two urls. Url for the one that works is http://localhost/api/Controller/GetFacilityName2

Url for the one that does not work is http://localhost/api/Controller/GetFacilityName?projectNumber=44

UPDATE I had a third method on the API that was this.

[HttpGet]
public Address GetWorkplaceAddress(string projectNumber)
{
     return new Address();
}

When I removed this from the API, the other method started to work. Address is defined as such.

public class Address
{
    public int Id { get; set; }
    public string AddressValue { get; set; }
    public string City { get; set; }
    public int? State { get; set; }
    public string Zip { get; set; }
    public int? Country { get; set; }
}

Upvotes: 1

Views: 25

Answers (1)

Igor
Igor

Reputation: 62213

You cannot overload an action on a controller by parameter which is how your code is currently written.

You can specify which action you want to occur by name using

  • the RouteAttribute
  • the route template routeTemplate: "api/{controller}/{action}/{id}",

OR you can specify it by the Http verb (Get,Post,Put,etc) which is what is currently being done.

Upvotes: 2

Related Questions