我将把我的字符串发布到api,但是每次我都有这样的错误。不过,被小提琴手弄得很好
NetworkError: 404 Not Found - http://localhost:3094/api/Controller/Action/这是我的js密码
var deferred = $q.defer();
$http.post('http://localhost:3094/api/Country/GetSelectionCount/' ,
{id:selection})
.success(function (data, status, headers, config) {
deferred.resolve(data);
})
.error(function (data, status, headers, config) {
deferred.reject(data);
});
return deferred.promise;和服务器代码
[AcceptVerbs("GET")]
[ActionName("GetSelectionCount")]
public IHttpActionResult GetSelectionCount(string id)
{
if (String.IsNullOrWhiteSpace(id))
return NotFound();
var result= (from m in db.Products
where m.ProductName.Contains(id)
select m).Count();
return Ok(result);
}发布于 2014-12-25 11:36:02
404在这种情况下是正常的。您正在从角投递一个具有id属性的对象,但是在服务器端,您需要一个string属性。因此,.net无法与动作相匹配。
您需要更改服务器端操作参数或角发送数据。
public class MyModel
{
public string id {get;set;}
}
[AcceptVerbs("POST")]
[ActionName("GetSelectionCount")]
public IHttpActionResult GetSelectionCount([FromBody] MyModel model)
{
if (model == null || String.IsNullOrWhiteSpace(model.id))
return NotFound();
var result= (from m in db.Products
where m.ProductName.Contains(model.id)
select m).Count();
return Ok(result);
}发布于 2014-12-25 13:23:07
我认为404与路由引擎有更多的关系,无法在控制器上找到相关的动作方法。您可以使用Route属性来确认它,如
[AcceptVerbs("POST")]
[ActionName("GetSelectionCount")]
[Route("api/country/GetSelectionCount/")]
public IHttpActionResult GetSelectionCount(string id)
{
if (String.IsNullOrWhiteSpace(id))
return NotFound();
var result= (from m in db.Products
where m.ProductName.Contains(id)
select m).Count();
return Ok(result);
}https://stackoverflow.com/questions/27646277
复制相似问题