我想實現在一個全新的web應用程序的WebAPI端點但無論我怎麼努力,我總是得到「404未找到」試圖做從GET時表示端點。的WebAPI端點總是給人「404未找到」
我開始簡單,只是試圖拉從數據庫中的車輛的清單。
我的應用程序的目錄結構看起來像這樣:
相關的,像這樣的代碼看起來位:
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
{
}
}
我試圖從大約每教程中,我能在網上找到的指示,我仍然沒有運氣好的話。
可以提供力所能及的幫助是極大的讚賞。
感謝,
伊恩
您是否嘗試過在API控制器上聲明'RoutePrefix'屬性? – JonE
我曾嘗試向** DataServiceController.cs **添加'[RoutePrefix(「dataservice」)]',但它沒有幫助。我也嘗試添加'[RoutePrefix(「dataservice/vehicles」)]'無濟於事。 – Ian