我无法弄清楚为什么会收到此错误。这是一个新项目,我使用的设置与我以前的项目相同。如果有人可以指出什么是不正确的。
错误消息
$id: "1"
Message: "No HTTP resource was found that matches the request URI 'http://localhost:55596 /api/apiClient'."
MessageDetail: "No type was found that matches the controller named 'apiClient'."
Webapi配置
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ClientApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { controller = "apiClient", id = RouteParameter.Optional }
);
}
app.js
'use strict';
var app = angular.module('YoungIntel', [
'ngResource',
'ngRoute',
'ui.bootstrap'
])
app.factory('Client', function ($resource) {
return $resource('/api/apiClient/:id',
{ id: '@id' },
{ 'save': { method: 'POST' } },
{ 'update': { method: 'PUT' } },
{ 'query': { method: 'GET'}});
});
app.factory('ClientGet', function ($http, $q) {
return {
query: function () {
var deferred = $q.defer();
$http({ method: 'get', url: '/api/apiClient' })
.success(function (data) {
deferred.resolve(data);
}).error(function (error) {
deferred.reject(error);
});
return deferred.promise;
}
}
});
控制器
//GET Clients
$scope.clientArray = {};
ClientGet.query().then(function (data) { $scope.clientArray = data; }, function (reason) { errorMngrSvc.handleError(reason); });
apiController
public class apiClientController : ApiController
{
IClient _adapter;
public apiClientController()
{
_adapter = new ClientDataAdapter();
}
public apiClientController(IClient adapter)
{
_adapter = adapter;
}
// GET api/<controller>
public IHttpActionResult Get()
{
var model = _adapter.GetClients();
return Ok(model);
}
// GET api/<controller>/5
public IHttpActionResult Get(int id)
{
Client client = new Client();
client = _adapter.GetClient(id);
if (client == null)
{
return NotFound();
}
return Ok(client);
}
// POST api/<controller>
public IHttpActionResult PostnewClient([FromBody]Client newClient)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var client = _adapter.PostNewClient(newClient);
return CreatedAtRoute("ClientApi", new { newClient.ClientId }, newClient);
}
// PUT api/<controller>/5
public HttpResponseMessage PutNewClient(int id, Client newClient)
{
ApplicationDbContext db = new ApplicationDbContext();
if (!ModelState.IsValid)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
if (id != newClient.ClientId)
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
db.Entry(newClient).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
// DELETE api/<controller>/5
public IHttpActionResult DeleteClient(int id)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
return Ok(_adapter.DeleteClient(id));
}
}
更新
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "ClientApi",
routeTemplate: "api/apiClient/{id}",
defaults: new { controller = "apiClient", id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
仍然得到相同的错误
答案 0 :(得分:1)
路线的顺序很重要。您正在注册2条相同的路线,以便执行第一场比赛。 切换到:
config.Routes.MapHttpRoute(
name: "ClientApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { controller = "apiClient", id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
然而,现在你的第一条路线将抓住一切。为避免这种情况,您需要将其更改为与默认路由不同。一种方法是将模板指定为api/apiClient/{id}
。
答案 1 :(得分:0)
路线api/apiClient
与您的两条路线相匹配。要修复它,只需删除第二个,您的路线将匹配第一个,并将到达控制器。