Multiple parameters in a web api 2 get

前端 未结 3 620
遇见更好的自我
遇见更好的自我 2021-02-01 19:32

I want to make a web api that is passed 4 parameters.

Here is my route:

        config.Routes.MapHttpRoute(
            name: \"DefaultApi\",
                    


        
3条回答
  •  梦如初夏
    2021-02-01 19:58

    public class Parameters
        {
            public int Param1 { get; set; }
            public string Param2 { get; set; }
        }
    

    and then in your controller method:

        [ActionName("DoSomething")]
        [HttpPost]
        public IHttpActionResult DoSomething(Parameters myParameters)
        {
            var x = myParameters.Param1;
            var y = myParameters.Param1;
           //do something else..
    
        }
    

    And build a ajax call like this:

    var request = {
                        Param1 : "1",
                        Param2 : "Mystring"
                    };
    
        function callToMethodController(request) {
            var deferred = $q.defer();
            $http.post('api/object/DoSomething', request)
                .success(function (data) {
                    deferred.resolve(data);
                }).error(function (error) {
                    deferred.reject('There was an error.');
                });
            return deferred.promise;
        }
    

提交回复
热议问题