【问题标题】:what's the issue here?? webapi + angular + post这里有什么问题?? webapi + 角度 + 帖子
【发布时间】:2017-05-17 02:08:42
【问题描述】:

发生了一些奇怪的事情......我真的不明白为什么...... 这篇文章只有在将 id 参数类型从 int 更改为 object 时才有效。

我知道最好的方法是在 url 中设置值......但是我试图避免这件事......我的偏好是将值发布为 json

public IHttpActionResult Gettest(int id) = 不工作!

{"Message":"找不到与请求 URI 'http://localhost:23052/api/testAPI/Gettest'匹配的 HTTP 资源。","MessageDetail":"在控制器 'testAPI' 上找不到与请求匹配的操作。"}

public IHttpActionResult Gettest(object id) = it works

运行良好

我做错了什么?....我尝试在服务器/客户端将参数名称从“id”更改为“x”,但结果相同。

config.Routes.MapHttpRoute("DefaultActionApi", "api/{controller}/{action}/{id}", new { id = RouteParameter.Optional });

WebApiConfig.cs

     [HttpPost]
    public IHttpActionResult Gettest(int id)
    {
        test test = db.tests.Find(id);
        if (test == null)
        {
            return NotFound();
        }

        return Ok(test);
    }

testAPIController.cs

        $http.post("/api/testAPI/Gettest", id)
    .success(function (result) {


        $scope.test = result;


    }).error(function (result) {
        console.log(result);
    });

角柱

【问题讨论】:

  • 如果你改用int?会怎么样?
  • 它没有工作... :S ...我刚刚尝试过
  • 更改为公共 IHttpActionResult Gettest([FromUri] int id)
  • 它不起作用....使它起作用的唯一方法...它将类型从 int 更改为 object.... #LOL
  • @JorgeSolano 你能试试我下面的建议吗.. 它应该可以工作..

标签: c# angularjs asp.net-mvc asp.net-web-api


【解决方案1】:

将您的 $http 请求更改为以下内容。不需要写动作方法名称,它只会被控制器名称调用。 Web api 基于 HTTP VERBS 工作,因此只需将您的请求替换为以下代码:

$http.post("/api/testAPI/"+ id)
        .success(function (result) { 
            $scope.test = result;
        }).error(function (result) {
            console.log(result);
        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多