Wesley Skeen
Wesley Skeen

Reputation: 8295

ASP.Net Web API custom route not working

I have the following function in a PersonController class

    [HttpGet]
    [ActionName("GetBloggersNotFollowed")]
    public IQueryable<object> GetBloggersNotFollowed(int companyId)
    {
        return Uow.People.GetPeople().Select(p => new { p.Email, p.FirstName, p.LastName, p.PhoneNumber, p.Type, p.UserName, p.Country, p.Id });
    }

It is used to retrieve a list of people.

I call the function as so

$.ajax({ 
   url: "/api/person/GetBloggersNotFollowed/1" 
}).success(function (people) { 
     PersonModule.GetPeople(people); 
});

And i have declared a route in my WebApiConfig.cs

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

When I try to call the route in the browser i get an error

<Error>
 <Message>
  No HTTP resource was found that matches the request URI 'http://localhost:1045/api/person/GetBloggersNotFollowed/1'.
  </Message>
  <MessageDetail>
       No action was found on the controller 'Person' that matches the request.
  </MessageDetail>
</Error>

I don't know here I went wrong. Can anyone see the problem?

Upvotes: 0

Views: 832

Answers (2)

Bhushan Firake
Bhushan Firake

Reputation: 9458

Replace your your route to this one:

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

This answer complements Mark Jones' answer.

Upvotes: 1

Mark Jones
Mark Jones

Reputation: 12194

The name of the parameter is important to the route matching.

You have named the parameter id in the route yet the method has it as companyId.

Either change {id} in the route to {companyId} or change companyId parameter to id.

Upvotes: 1

Related Questions