0

I'm new to angular and while trying post a string to web api from angular project,the parameter in the web api Post method is always null.If i use [FromBody], the post method itself not getting called.If i remove the [FromBody] from the Post method .The method is getting called but the value of the parameter is null.Can anyone please help me here!!

SendTODB(db:Event)
{
  
  this.http.post('https://localhost:44301/Student',this.inpuctstexts).subscribe((data)=>(<HTMLInputElement>db.target).value);
} 

In My WebApi:

[ApiController]
    [Route("[controller]")]
    public class StudentController : ControllerBase
    {
        [HttpGet]
        public ActionResult<List<Student>> StudentList()
        {
            List<Student> student = new List<Student>();
            student.Add(new Student() { StudentName = "Mike", Marks = "200" });
            student.Add(new Student() { StudentName = "Jack", Marks = "250" });
            student.Add(new Student() { StudentName = "Jacob", Marks = "300" });
            student.Add(new Student() { StudentName = "John", Marks = "500" });
            return (student);

        }

        [HttpPost]
        public ActionResult<string> student(string input)
        {
            return input;
        }
    }

This is my angular code

This is my WebApi Controller class

Vishnuraj_
  • 5
  • 1
  • 5

3 Answers3

0

HttpClient probably uses application/json as content-type by default so try wrapping your payload in an object on both ends.

M.Samir
  • 1
  • 1
  • Hi Mohamed ..I have used json.Stringify(this..inpuctstexts).But invain.The parameter in the post method is null – Vishnuraj_ Aug 06 '20 at 13:20
0

This is what formbody does:

When a parameter has [FromBody], Web API uses the Content-Type header to select a formatter. In this example, the content type is "application/json" and the request body is a raw JSON string (not a JSON object). At most one parameter is allowed to read from the message body.

please check this answer:

FromBody string parameter is giving null

0

I think you need to set the header and [FromBody] will be required at the API. Change your service code to

SendTODB(db:Event)
{
   const header = {
 'Content-Type': 'application/json',
  'Accept': 'application/json',
  'Access-Control-Allow-Headers': 'Content-Type',
    }
    
   const headerOptions = {                                                                                                                                                                                 
     headers: new Headers(header), 
   };
    
   const data = JSON.stringify(inpuctstexts);

   return this.http.post(https://localhost:44301/Student, data, headerOptions)
          .subscribe((data)=> {
                  // your code will go here
               }) ;
}

If it not work then create a model in you api

    class RequrestModel{
    
    public string input {get;set;}
    }


 [HttpPost]
        public ActionResult<string> student([FromBody]RequestModel request)
        {
            return request.input;
        }
  

From your front end

SendTODB(db:Event)
    {
           const header = {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
      'Access-Control-Allow-Headers': 'Content-Type',
        }
        
       const headerOptions = {                                                                                                                                                                                 
         headers: new Headers(header), 
       };

      const dataModel = {
       input = inpuctstexts;
       }
        
       const data = JSON.stringify(dataModel);
    
       return this.http.post(https://localhost:44301/Student, data, headerOptions)
              .subscribe((data)=> {
                      // your code will go here
                   }) ;
    }

It will work