我正在尝试在全新的Web应用程序中实现WebApi端点,但是无论尝试什么,当我尝试从所述端点执行GET时,总是会收到"404 not found".
I am trying to implement WebApi endpoints in a brand new web app but no matter what I try I always get "404 not found" when trying to do a GET from said endpoint.
我从简单开始,只是尝试从数据库中提取车辆列表.
I'm starting simple and just trying to pull a list of vehicles from the database.
我的应用程序的目录结构如下:
The directory structure of my application looks like so:
相关代码如下:
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 { } }我已经尝试过我可以在网上找到的每个教程中的说明,但是仍然没有运气.
I've tried the instructions from about every tutorial I could find online and I'm still not having any luck.
我们将不胜感激.
谢谢
伊恩
推荐答案我们有一个答案!
Dan Dumitru和jps都是正确的.在尝试了IIS Express之后,我意识到了我的错误.
Dan Dumitru and jps were both correct. After trying IIS Express I realized my error.
端点确实是 localhost/TeleAiDiagnostics/api/dataservice/vehicles ,而不仅仅是 localhost/api/dataservice/vehicles .
The endpoint is indeed localhost/TeleAiDiagnostics/api/dataservice/vehicles and not simply localhost/api/dataservice/vehicles.
不确定为什么我花了这么长时间才意识到这一点.无论如何,我要感谢大家的帮助.
Not sure why it took this long for me to realize this. Regardless, I want to thank everyone for their help.
更多推荐
WebApi端点始终给出"404 not found".
发布评论