2016-06-21 70 views
0

所以我,做來回在同一個MVC應用程序爲m角碼到web API控制裝置的呼叫,像這樣角調用的WebAPI不打斷點

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

    }); 

它工作正常了,居然打Web API實際用於獲取數據的類中的斷點,但不會觸及控制器。 T'm不傳遞一個id,所以它打到第二個不帶參數的httpget。控制器在下面。注意:LimeLoader()。GetLimes()行是在解決方案中調用一個類來調用數據。我在這堂課中有一個斷點,它很好。

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

,路線如下:

 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 } 
     ); 
    } 

從第二HTTPGET方法返回集合,但爲什麼沒有被擊中控制器我的斷點?提前致謝。

+0

問題真的很混亂。你在哪裏設定了斷點? – Win

+0

你能顯示你的路線配置嗎?如果您使用web api 2,我發現使用屬性路由更容易。 – Ionian316

+0

在發佈之前,您需要重讀您的問題。你在哪裏試圖通過Angular傳遞「id」? (); – granadaCoder

回答

0

試試這個: WebApiConfig.cs

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

角:

$http.get('/api/lime/GetLimes').success(function (data){.. 
0

我解決它通過添加完整的URL路徑的API調用,即

.. /../api

而不是/ api

不確定爲什麼這仍然執行,如果你不提供完整的路徑,但它確實如此。