0

So I'm, making a call to a web api controller fro within the same mvc app as m angular code, like so

        $http.get('/api/lime/').success(function (data) {
        $scope.line = data;
        $scope.$apply();
    }).error(function (jqXhr, status, errorThrown) {
        //$('#lblMessage').text(errorThrown).show();

    });

It works fine, and actually hits the break point in the class the the web api actually uses to get the data, but it won't hit the controller. T'm not passing an id, so it's hitting the second httpget that doesn't take a parameter. The controller is below. Note: The LimeLoader().GetLimes() line is a call to a class in the solution that pulls in the data. I have a breakpoint in this class and it reaches it fine.

    public class LimeController : ApiController
{
    [HttpGet]
    public LimeLoader GetLime(int id)
    {
        return new LimeLoader(id);
    }
    [HttpGet]
    public IEnumerable<LimeLoader> GetLimes()
    {
        return new LimeLoader().GetLimes().ToList();
    }
}

Here's the route:

        public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        // Web API routes
        config.MapHttpAttributeRoutes();

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

It returns the collection from the second HttpGet method, but why isn't is hitting my breakpoint in controller? Thanks in advance.

user609926
  • 801
  • 2
  • 12
  • 25
  • Question is really confusing. Where did you set the break point? – Win Jun 21 '16 at 13:41
  • Can you show your route configuration? I find it easier to use attribute routing if you are using web api 2. – Ionian316 Jun 21 '16 at 13:49
  • You need to reread your question before you post it. Where are you trying to pass the "id" via the Angular? – granadaCoder Jun 21 '16 at 13:51
  • I set one breakpoiint at the "return LimeLoader().GetLimes().Tolist(); – user609926 Jun 21 '16 at 13:51
  • Possible duplicate of [Custom method names in ASP.NET Web API](http://stackoverflow.com/questions/9569270/custom-method-names-in-asp-net-web-api) – Igor Jun 21 '16 at 14:01
  • It has to do with your method names and the lack of routing information. Your Web API does not know where to route the request. This is a duplicate of [Custom method names in ASP.NET Web API](http://stackoverflow.com/a/9569381/1260204) – Igor Jun 21 '16 at 14:02
  • But why would it execute the second method an return the data? Is it just that I can't set a breakpoint there even though it hits that method, because I didn't properly set the routing? – user609926 Jun 21 '16 at 14:55

2 Answers2

0

Try this: WebApiConfig.cs

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

Angular:

 $http.get('/api/lime/GetLimes').success(function (data){..
riteshmeher
  • 854
  • 8
  • 16
0

I solved it by adding the full url in the path to the api call, ie

../../api

instead of /api

Not sure why this still executes if you don't provide the full path, but it does.

user609926
  • 801
  • 2
  • 12
  • 25