2017-07-28 54 views
1

我想實現在一個全新的web應用程序的WebAPI端點但無論我怎麼努力,我總是得到「404未找到」試圖做從GET時表示端點。的WebAPI端點總是給人「404未找到」

localhost/api/dataservice/vehicles not found

我開始簡單,只是試圖拉從數據庫中的車輛的清單。

我的應用程序的目錄結構看起來像這樣:

directory structure

相關的,像這樣的代碼看起來位:

dataService.js

(function() { 

var injectParams = ['vehiclesService']; 

var dataService = function (vehiclesService) { 
    return vehiclesService; 
}; 

dataService.$inject = injectParams; 

angular.module('teleAiDiagnostics').factory('dataService', dataService); 
}()); 

vehiclesService .js文件

(function() { 

var injectParams = ['$http', '$q']; 

var vehiclesFactory = function ($http, $q) { 
    var serviceBase = '/api/dataservice/', 
     factory = {}; 

    factory.getVehicles = function() { 
     return $http.get(serviceBase + 'vehicles').then(function (response) { 
      var vehicles = response.data; 
      return { 
       totalRecords: parseInt(response.headers('X-InlineCount')), 
       results: vehicles 
      }; 
     }); 
    }; 

    return factory; 
}; 

vehiclesFactory.$inject = injectParams; 

angular.module('teleAiDiagnostics').factory('vehiclesService', vehiclesFactory); 
}()); 

DataServiceController.cs

using System.Collections.Generic; 
using System.Net; 
using System.Net.Http; 
using System.Web; 
using System.Web.Http; 

namespace TeleAiDiagnostics 
{ 
    public class DataServiceController : ApiController 
    { 
     TeleAiRepository _TeleAiRepository; 

     public DataServiceController() 
     { 
      _TeleAiRepository = new TeleAiRepository(); 
     } 

     [HttpGet] 
     public HttpResponseMessage Vehicles() 
     { 
      List<TeleAiVehicle> vehicles = _TeleAiRepository.GetVehicles(); 
      HttpContext.Current.Response.Headers.Add("X-inlineCount", vehicles.Count.ToString()); 
      return Request.CreateResponse(HttpStatusCode.OK, vehicles); 
     } 
    } 
} 

vehiclesController.js

(function() { 
    var injectParams = ['$location', 'dataService']; 

    var VehiclesController = function ($location, dataService) { 
     var vm = this; 

     vm.vehicles = []; 

     function init() { 
      dataService.getVehicles() 
       .then(function (data) { 
        vm.vehicles = data.results; 
       }, function (error) { 
        var thisError = error.data.message; 
       }); 
     }; 

     init(); 
    }; 

    VehiclesController.$inject = injectParams; 

    angular.module('teleAiDiagnostics').controller('VehiclesController', VehiclesController); 
}()); 

WebApiConfig.cs

using Newtonsoft.Json; 
using Newtonsoft.Json.Serialization; 
using System.Linq; 
using System.Web.Http; 
using System.Web.Routing; 

namespace TeleAiDiagnostics 
{ 
    public static class WebApiConfig 
    { 
     public static void Register(HttpConfiguration config) 
     { 

      config.MapHttpAttributeRoutes(); 

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

      config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 
      config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; 

      // Remove default XML handler 
      var matches = config.Formatters 
           .Where(f => f.SupportedMediaTypes 
              .Where(m => m.MediaType.ToString() == "application/xml" || 
                 m.MediaType.ToString() == "text/xml") 
              .Count() > 0) 
           .ToList(); 

      foreach (var match in matches) 
       config.Formatters.Remove(match); 
     } 
    } 
} 

的Global.asax.cs

using System; 
using System.Web.Http; 

namespace TeleAiDiagnostics 
{ 
    public class Global : System.Web.HttpApplication 
    { 

     protected void Application_Start(object sender, EventArgs e) 
     { 
      WebApiConfig.Register(GlobalConfiguration.Configuration); 
      GlobalConfiguration.Configuration.EnsureInitialized(); 
     } 

     protected void Session_Start(object sender, EventArgs e) 
     { 

     } 

     protected void Application_BeginRequest(object sender, EventArgs e) 
     { 

     } 

     protected void Application_AuthenticateRequest(object sender, EventArgs e) 
     { 

     } 

     protected void Application_Error(object sender, EventArgs e) 
     { 

     } 

     protected void Session_End(object sender, EventArgs e) 
     { 

     } 

     protected void Application_End(object sender, EventArgs e) 
     { 

     } 
    } 
} 

RouteConfig.cs

namespace TeleAiDiagnostics 
{ 
    public class RouteConfig 
    { 
    } 
} 

我試圖從大約每教程中,我能在網上找到的指示,我仍然沒有運氣好的話。

可以提供力所能及的幫助是極大的讚賞。

感謝,

伊恩

+1

您是否嘗試過在API控制器上聲明'RoutePrefix'屬性? – JonE

+0

我曾嘗試向** DataServiceController.cs **添加'[RoutePrefix(「dataservice」)]',但它沒有幫助。我也嘗試添加'[RoutePrefix(「dataservice/vehicles」)]'無濟於事。 – Ian

回答

2

我們有一個答案!

丹杜米特魯和JPS都是正確的。嘗試IIS Express後,我意識到我的錯誤。

端點確實是http://localhost/TeleAiDiagnostics/api/dataservice/vehicles而不僅僅是http://localhost/api/dataservice/vehicles

不知道爲什麼花了這麼長時間才意識到這一點。無論如何,我想感謝大家的幫助。

+1

謝謝,但在我回家的路上重新考慮之後,我認爲我對端口的想法是錯誤的,因爲如果端口號錯誤,您將得到一個不同的錯誤(50x?),因爲客戶端將無法訪問任何服務。無論如何,很高興聽到它解決了:)。 – jps

+0

你的回答仍然指向我正確的方向。 – Ian

1

這可能是因爲你的默認Web API路線。我認爲應該是:

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

(不{action}部分)

+0

我已經改變了它看起來像這樣: 'config.Routes.MapHttpRoute( 名稱: 「DefaultApi」, routeTemplate: 「API/{}控制器/(編號)」,違約 :新{ID = RouteParameter .Optional} );' 仍然沒有骰子。 我也累了修改** vehiclesService.js **以消除'dataservice'部分,但它仍然無法正常工作。 – Ian

+1

@Ian - 如果你只是從你的瀏覽器轉到'http:// localhost/api/dataservice',你還會得到404嗎? –

+0

我仍然得到了404。'http:// localhost/api/dataservice/vehicles'和'http:// localhost/api/vehicles'也是同樣的東西。 – Ian