locked
Help Needed in API URL access RRS feed

  • Question

  • User1489758560 posted

    Hi,

    I have created a Rest Services using web API2 and I am able to work with Get method. Having trouble to use POST and PUT. Question below,

    How can i implement the multiple POST and PUT method in same controller?

    public class User
        {        
            public string UserName { get; set; }        
            public string Password { get; set; }
        }


    public class UserPin
        {        
            public string UserName { get; set; }        
            public string Pin{ get; set; }
        }
    


    on my UserController, i have below methods

    [HttpPut]
            [Route("ChangePassword")]
            public HttpResponseMessage ChangePassword(User ObjUser)
            { 
    ...... 
    
    }
    
    [HttpPut]
            [Route("ChangePin")]
            public HttpResponseMessage ChangePin(UserPin ObjUserPin)
            { 
    ...... 
    
    }

    when i try to execute, i am always getting null on the input parameters.

    http://localhost:40744/api/user/ChangePassword

    request body :

    UserName = "Test" & password="Test123"

    http://localhost:40744/api/user/ChangePin

     request body:

    UserName = "Test" & Pin="123"

    Any suggestion how to implement this please and please help on achieving the multiple POST methods in the same class.

    thakns

    Tuesday, May 12, 2015 6:49 PM

Answers

  • User-782957977 posted

    You don't need to deserialize. Web Api will do it and bind parameters from body to user object. Please use like this

    [HttpPut]
            [Route("ChangePassword")]
            public void ChangePassword([FromBody]User  ObjUser)
            { 
    
               //ObjUser.UserName
               //ObjUser.Password
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 12, 2015 10:21 PM

All replies

  • User-782957977 posted

    Try like this

    Client code to call web api

     using (var client = new HttpClient())
                 {
                     client.BaseAddress = new Uri("http://localhost:60122");
                     User user = new User(){UserName = "UserName",Password = "Password"};
                     var response = client.PutAsJsonAsync("ChangePassword", user).Result;
                     if (response.IsSuccessStatusCode)
                     {
                         string responseString = response.Content.ReadAsStringAsync().Result;
                     }
                 }

    Web Api code

     [HttpPut]
            [Route("ChangePassword")]
            public void ChangePassword([FromBody]User  ObjUser)
            { 
    
    
            }
    Tuesday, May 12, 2015 8:32 PM
  • User1489758560 posted

    Hi Santhosh,

    Thanks for you reply and Please advice me do i need to deserialize the what i receive in FromBody? Can you please  use my sample code  to achieve this.

    thakns

    Tuesday, May 12, 2015 9:18 PM
  • User-782957977 posted

    You don't need to deserialize. Web Api will do it and bind parameters from body to user object. Please use like this

    [HttpPut]
            [Route("ChangePassword")]
            public void ChangePassword([FromBody]User  ObjUser)
            { 
    
               //ObjUser.UserName
               //ObjUser.Password
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 12, 2015 10:21 PM
  • User1489758560 posted

    Thanks, even without from body also working.

    Wednesday, May 13, 2015 6:17 PM