How to get data from POST request in ASP NET WEB API CORE?

Hi all. Situation: want to make your website Google-authorization, and if a user with such a aydishnik there is a record in the database a new ID. All scripts already setup, but ran into a problem when using jQuery.Ajax send to Web API data, it correctly fails to take in connection with the lack of knowledge and more detailed documentation. The only way I was able to receive data from the data - this is the elementary cause of type string the entire data request.
My Ajax request
$.ajax({
 type: "POST",
 url: "https://localhost:5001/api/User/Check_User",
 data: "googleid='" + val + "'",
 success: function (msg) {
 console.log("data Arrived:" + msg);
}
 });

C# ASP NET WEB API CORE:
[HttpPost()]
 public IActionResult Check_User()
{
 StreamReader sr = new StreamReader(Request.Body);
 string data = sr.ReadToEnd();
 string googleid = data;
 return new ObjectResult(googleid);
 }

that would correctly translate to the keys my data will have to use regular expressions, but I have concerns what I'm doing wrong and can this all be done much easier. Help please, I want to know how can I post a request to get the data that I send ajax request
March 19th 20 at 09:25
1 answer
March 19th 20 at 09:27
[HttpPost()]
public IActionResult Check_User([FromBody] YourModel model)
{
 return new ObjectResult(model.Googleid);
}

public class YourModel
{
 string Googleid {get; set;}
}

Try this
tried. 404 error
My model
[Table("Users")]
 public class Person
{
 public int Id { get; set; }
 public string Google_Id { get; set; }
 public string First_Name { get; set; }
 public string Last_Name { get; set; }
 public string Middle_Name { get; set; }
 public string Date_Born { get; set; }
 public string Email { get; set; }
 public int Gender { get; set; }
 public string Name_Of_Character { get; set; }
 public int Gender_Of_Character { get; set; }
 public string Date_Born_Of_Character { get; set; }
 public int Completed_Tasks { get; set; }
 public int Rating_Of_Eloquence { get; set; }
 }
- Jettie_Wiegand commented on March 19th 20 at 09:30
So there you have Google_Id, and send Googleid. It will not sypitsya - athena commented on March 19th 20 at 09:33
@athena, Now tried the same name to do the same. Here's what else I tried to do that didn't help either
var data = {
Id:0,
 Google_Id: val,
 First_Name: ",
 Last_Name: ",
 Middle_Name: ",
 Date_Born: ",
 Email: ",
 Gender: 0,
 Name_Of_Character: ",
 Gender_Of_Character: 0,
 Date_Born_Of_Character: ",
 Completed_Tasks: 0,
Rating_Of_Eloquence:0
}
console.log(data);
$.ajax({
 type: "POST",
 url: "https://localhost:5001/api/User/Check_User",
 data: data,
 contentType: 'application/json; charset=utf-8',
 dataType: 'json',
 success: function (msg) {
 console.log("data Arrived:" + msg);
}
 });
- Jettie_Wiegand commented on March 19th 20 at 09:36
@Jettie_Wiegand, also 404? - athena commented on March 19th 20 at 09:39
@athena, I did it)
Here's how it was:
[HttpPost()]
 public IActionResult Check_User(Person model)
{


 return new JsonResult(model.Google_Id);
 }

var data = {
 Google_Id: val
}
//console.log(data);
$.ajax({
 type: "POST",
 url: "https://localhost:5001/api/User/Check_User",
 data: JSON.stringify(data),
 contentType: 'application/json',
 success: function (msg) {
 console.log("data Arrived:" + msg);
}
 });
- Jettie_Wiegand commented on March 19th 20 at 09:42
@Jettie_Wiegand, all I thought is optional in the JSON on the client data to distill. But Oh well - athena commented on March 19th 20 at 09:45
@athena, Yes, optional. Also if I put ObjectResult(model.Google_Id) - will work. - Jettie_Wiegand commented on March 19th 20 at 09:48
@Jettie_Wiegand, and, well, OK. About ObjectResult(model.Google_Id) I in the answer and written) - athena commented on March 19th 20 at 09:51

Find more questions by tags Web serversC#ASP.NET