Я создаю веб-приложение для использования REST FULL API Базовая веб-служба ASP.Net, у меня проблема при обновлении записей.
я пытаюсь вызвать метод Put из ядра API ASP.Net приложением ASP.Net Core
У меня есть исключение NullReferenceException: ссылка на объект не установлена на экземпляр объекта.
public async Task<IActionResult> UpdateEmployee(int id)
{
Employee employee = new Employee();
using(var httpClient=new HttpClient())
{
using(var response = await httpClient.GetAsync("myURI/employee/" + id))
{
string apiResponse = await
response.Content.ReadAsStringAsync();
employee = JsonConvert.DeserializeObject<Employee>apiResponse);
}
}
return View(employee);
}
[HttpPost]
public async Task<IActionResult> UpdateEmployee(Employee employee)
{
Employee receivedemployee = new Employee();
using(var httpClient=new HttpClient())
{
var content = new MultipartFormDataContent();
content.Add(new
StringContent(employee.EmployeeId.ToString(),Encoding.UTF8,
"application/json"), "id");
content.Add(new
StringContent(employee.FirstName,Encoding.UTF8,
"application/json"),"FirstName");
content.Add(new StringContent(employee.LastName,
Encoding.UTF8, "application/json"), "LastName");
content.Add(new StringContent(employee.DateOfBirth.ToString(),
Encoding.UTF8, "application/json"), "Email");
content.Add(new StringContent(employee.PhoneNumber,
Encoding.UTF8, "application/json"), "DateOfBirth");
content.Add(new StringContent(employee.Email, Encoding.UTF8,
"application/json"), "Email");
using (var response = await httpClient.PutAsync("myURI/api/employee", content))
{
string apiResponse = await response.Content.ReadAsStringAsync();
ViewBag.Result = "Success";
receivedemployee = JsonConvert.DeserializeObject<Employee>(apiResponse);
}
return View(receivedemployee);
}
}
}
я ожидал обновления записи