asp.net-web-api2angular5typescript-2.5

Multiple parameter in HTTP post request not Binding in Asp.net webApi 2.0


I have created a sample for post multiple params in HTTP Request.But i can't able to assign the value in Web Api Controller. Please check the below code.

.ts

deleteEntry(entryId: number, entryActiveFlag: string): Observable<number> {
        let dataUrl = `http/localhost:8080/Entry/DeleteEntry`;
        let params = { entryId, entryActiveFlag};
        //params.append(entryId);
        //params.append('entryActiveFlag', entryActiveFlag);
        let body = JSON.stringify(params );
        return this.http.post(dataUrl, body)
            .map(res => res.json())
            .catch(this.handleError);
    }

.WebApi

 [HttpPost("DeleteEntry")]
    public  IActionResult DeleteEntry([FromBody]int entryId,string entryActiveFlag)
    {
        return Ok( _service.DeleteEntry(entryId, entryActiveFlag));
    }

Solution

  • Wrap the input details in one class and provide the class object as parameter to Web API method. In your case you can add below class -

    public class EntryDetails
    {
      public int EntryId {get;set;}
    
      public bool EntryActiveFlag
    
    }
    

    And your web method will be

    [HttpPost("DeleteEntry")]
        public  IActionResult DeleteEntry(EntryDetails entryDetails)
        {
            return Ok( _service.DeleteEntry(entryDetails.EntryId, entryDetails.EntryActiveFlag));
        }
    

    Accordingly also change the invocation from client method.