c# - WebApi endpoint always gives "404 not found" -
i trying implement webapi endpoints in brand new web app no matter try "404 not found" when trying said endpoint.
i'm starting simple , trying pull list of vehicles database.
the directory structure of application looks so:
the relevant bits of code 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 instructions every tutorial find online , i'm still not having luck.
whatever can provided appreciated.
thanks,
ian
we have answer!
dan dumitru , jps both correct. after trying iis express realized error.
the endpoint indeed http://localhost/teleaidiagnostics/api/dataservice/vehicles , not http://localhost/api/dataservice/vehicles.
not sure why took long me realize this. regardless, want thank help.


Comments
Post a Comment