【问题标题】:How to execute PUT method from angularJS to web api?如何执行从 angularJS 到 web api 的 PUT 方法?
【发布时间】:2014-08-08 06:33:16
【问题描述】:

我真的不知道如何使用 web api 的 PUT 方法从角度传递参数。

我了解 GET 方法是如何执行的,但 PUT、POST 和 DELETE 对我来说真的很难。

我看了很多文章,但还是不明白

我的 web api 的控制器中有这样的代码:

static readonly IProfile profileRepository = new ProfileRepository();
    [Route("api/profile/")]
    [HttpGet]
    [System.Web.Http.AcceptVerbs("GET")]

    public IEnumerable<Profile> getProfiles()
    {
        return profileRepository.getProfiles();
    }

    [Route("api/profile/")]
    [HttpPut]
    [System.Web.Http.AcceptVerbs("PUT")]
    public IEnumerable<Profile> putProfile(Profile profile)
    {
        profileRepository.putProfile(profile);
        return getProfiles();
    }

我的 angularJS 中也有这样的服务

 var _putProfile = function (name,address,contacts) {
        return $http.put(serviceURL + 'api/profile/Name=' + name + '&Address=' + address + '&Contact=' + contacts).then(function (results) {
            return results;
        });
    };

当我使用Postman 应用程序时,当我使用x-www-form-urlencoded 时,web api 执行良好,它将数据从邮递员传递到 web api,但如何将数据从 angularJS 传递到 web api。

有没有人可以在这里给我一个最佳答案..请给我一个想法如何做到这一点,并请告诉我什么是最佳做法..

我只是 angularJS 和 Web Api 的新手,请指导我.. 非常感谢

【问题讨论】:

    标签: asp.net angularjs asp.net-web-api asp.net-web-api2


    【解决方案1】:

    AngularJS 发送 json 数据而不是 x-www-form-urlencoded 格式数据。 Web API 具有读取两者的能力。 对于 HTTP PUT 动词,数据应该在正文而不是查询字符串中传递。

    对于您的 $http.put 调用,您应该这样做。

    $http.put(serviceURL + 'api/profile', { Name:name, Address:address, Contact:contacts});

    请阅读 $http 文档。

    【讨论】:

      猜你喜欢
      • 2015-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      相关资源
      最近更新 更多