Home > database >  How can I get data from a JSON file in .net, that comes thorugh an HTTP PUT request from Angular?
How can I get data from a JSON file in .net, that comes thorugh an HTTP PUT request from Angular?

Time:04-14

I have a project, and I have to rewrite the Frontend from CSHTML to Angular. I set up the register page in Angular, and using a PUT request I can send the data to the backend. But I can't seem to extract the different variables from it. My main problem is that I don't know how to acces the file that comes in when I send the PUT request. The request:

register() {
    var body = {
      ID: '',
      firstName: this.registerModel.value.firstName,
      lastName: this.registerModel.value.lastName,
      gender: this.registerModel.value.gender,
      email: this.registerModel.value.email,
      password: this.registerModel.value.password
    };
    return this.http.post(this.apiUrl   '/Register/Register', body)
  }

I call the function when the user submits the form:

onSubmit(){
    console.log("clicked");
    this.service.register().subscribe()
  }

And the function that it calls looks through the Json where the existing users are stored, and should compare them (return Json values are only for testing):

[HttpPost]
        public IActionResult Register(User user)
        {
            
            string path = @"./Data/Users.json";
            //Instantiate User
            User newUser = new User();
            Guid ID = Guid.NewGuid();

            newUser.ID = ID;
            newUser.firstName = user.firstName;
            newUser.lastName = user.lastName;
            newUser.email = user.email;
            newUser.gender = user.gender;
            newUser.password = user.password;

            if (user.firstName == null || user.lastName == null
                || user.email == null || user.gender == null || user.password == null)
            {
                ViewData["message"] = "Please Fill all fields!";
                var data = "null in variables";
                return Json(new { data = data });
            }

            string fileContent;

            if (System.IO.File.Exists(path))
            {
                fileContent = System.IO.File.ReadAllText(path);
                users = JsonConvert.DeserializeObject<List<User>>(fileContent);

                foreach (User current in users)
                {
                    if (current.email == newUser.email)
                    {
                        ViewData["message"] = "Already exists!";
                        var data3 = "existing user";
                        return Json(new { data3 = data3 });
                    }
                }

                users.Add(newUser);

                string json = JsonConvert.SerializeObject(users);
                System.IO.File.WriteAllText(path, json);
            }

            var data2 = "succes";
            return Json(new { data2 = data2 });
        }

CodePudding user response:

We were able to solve the problem. I hat to add a few modifications to the controller, and the Startup.cs file. I will post code snippets when I can access the PC the code is on. But shortly: I had to add APIController and routing to the controller, the function needed a fromBody tag, and I had to change a line in the Startup.Cs file. Now it runs perfectly.

  • Related