简体   繁体   中英

How to pass multiple post parameters to web api using angular typescript

I would like to pass multiple values to the following web api using angularjs typescript.

// POST api/values
        public void Post([FromBody]string value1, [FromBody]string value2)
        {
        }

I would like to call the above method something like this

$http.post('api/values', ???)

As I need to do some validations on the page by passing multiple parameters to the database. I also tried with GET instead of post but didn't work for me.

Please share your thoughts.

Thank you. Hari C

You can't read multiple values "FromBody". Instead you should define "Request" class with all needed parameters:

    public class Request
    {
        public string Value1 { get; set; } 
        public string Value2 { get; set; } 
    }

    //POST api/values
    public void Post([FromBody]Request request)
    {
    }

And then like Aran said you can go this way

$http.post('api/values', {Value1:"foo", Value2:"bar"});

使用data属性( $http.post的第二个参数)传递参数:

$http.post('api/values', {x:"foo", y:"bar"});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM