简体   繁体   English

对WEBAPI的角度调用未达到断点

[英]Angular call to WEBAPI not hitting breakpoint

So I'm, making a call to a web api controller fro within the same mvc app as m angular code, like so 因此,我在与m角度代码相同的mvc应用程序中来回调用Web api控制器,如下所示

        $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. 它工作正常,并实际上达到了Web API实际用于获取数据的类中的断点,但不会达到控制器的目的。 T'm not passing an id, so it's hitting the second httpget that doesn't take a parameter. T没有传递id,因此它命中了第二个不带参数的httpget。 The controller is below. 控制器在下面。 Note: The LimeLoader().GetLimes() line is a call to a class in the solution that pulls in the data. 注意:LimeLoader()。GetLimes()行是对解决方案中的类的调用,该类提取数据。 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? 它从第二个HttpGet方法返回集合,但是为什么没有在控制器中达到断点呢? Thanks in advance. 提前致谢。

Try this: WebApiConfig.cs 试试这个: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){..

I solved it by adding the full url in the path to the api call, ie 我通过在api调用的路径中添加完整URL来解决它,即

../../api ../../api

instead of /api 代替/ api

Not sure why this still executes if you don't provide the full path, but it does. 不知道如果不提供完整路径,为什么仍然执行此操作,但确实如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM