Angular 调用 WebApi 传参

转载地址:https://blog.csdn.net/yo548720570/article/details/40400259

WebApi的CURD方法记录以及调用传参

  1. // GET api/test
  2. public IEnumerable<string> Get()
  3. {
  4. return new string[] { "value1", "value2" };
  5. }
  6. // GET api/test/5 { id: "5" }
  7. public string Get(string id)
  8. {
  9. return "value";
  10. }
  11. // PUT api/test [{ Id: "11", Value: "22" },{ Id: "33", Value: "44" }]
  12. public void Post([FromBody]IEnumerable<Model> list)
  13. {
  14. }
  15. // PUT api/test [{ Id: "11", Value: "22" },{ Id: "33", Value: "44" }]
  16. public void Put([FromBody]IEnumerable<Model> list)
  17. {
  18. }
  19. // DELETE api/test?id=5 { id: "5" }
  20. public void Delete(string id)
  21. {
  22. }
  23. // DELETE api/test?json=%5B%7B%22Id%22:%2211%22%7D,%7B%22Id%22:%2233%22%7D%5D { json: JSON.stringify([{ Id: "11" }, { Id: "33" }]) }
  24. public void Deletes(string json)
  25. {
  26. JArray jsonObj = JArray.Parse(json);
  27. }

调用及传参:

  1. $scope.resultValue = service.Get(); //Get
  2. $scope.resultValue = service.Get({ id: "123"}); //Get(string id)
  3. $scope.resultValue = service.Post([{ Id: "11", Value: "22" }, { Id: "33", Value: "44" }]); //Post([FromBody]IEnumerable<Model> list) 不能与单个参数的Post共存
  4. $scope.resultValue = service.Put([{ Id: "11", Value: "22" },{ Id: "33", Value: "44" }]); //Put([FromBody]IEnumerable<Model> list)
  5. $scope.resultValue = service.Delete({ id: "123" }); //Delete(string id)
  6. $scope.resultValue = service.Delete({ json: JSON.stringify([{ Id: "11" }, { Id: "33" }]) }); //Deletes(string json)

angular服务:

  1. angular.module( 'services', [ 'ngResource'])
  2. .factory( 'service', function ($resource) {
  3. return $resource( 'http://localhost:25397/api/test/', {}, {
  4. GetJsonp: { method: 'JSONP', params: { callback: 'JSON_CALLBACK' }, isArray: true },
  5. Get: { method: 'GET', isArray: true },
  6. Post: { method: 'POST' },
  7. Put: { method: 'Put'},
  8. Delete: { method: 'Delete'}
  9. })
  10. })


http://techbrij.com/pass-parameters-aspdotnet-webapi-jquery

猜你喜欢

转载自blog.csdn.net/xwnxwn/article/details/80977163