Я создал два проекта для веб-API и основного приложения asp.net.Я хочу реализовать веб-API в моем приложении.
Метод "PUT" из моего веб-API:
[Consumes("application/json")]
[HttpPut]
public async Task<IActionResult> PUT(Employee employee)
{
try
{
await employeeRepository.UpdateEmployeeByProperty(
employee, e => e.FirstName,
e => e.MiddleName, e => e.LastName,
e => e.DateOfBirth, e => e.ContactNo,
e => e.EmailId, e => e.HighestQualification,
e => e.TypeOfEmployee, e => e.Gender,
e => e.StartDate, e => e.EndDate);
if (await employeeRepository.SaveChangesAsync())
{
return Ok();
}
else
{
return BadRequest();
}
}
catch (Exception)
{
return BadRequest();
}
}
, и я вызываю вышеуказанный метод в моем приложении, используя следующий код:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> EditEmployee(Employee employeeModel,string email,int id)
{
var UserWithSameEmail = (Employee)null;
if(employeeModel.EmailId!=email)
UserWithSameEmail = _dbContext.Employee
.Where(e => e.EmailId == employeeModel.EmailId).SingleOrDefault();
if (ModelState.IsValid)
{
employeeModel.UpdatedDate = DateTime.Now;
if (UserWithSameEmail == null)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:50822");
var json = JsonConvert.SerializeObject(employeeModel);
HttpResponseMessage response = client
.PutAsJsonAsync("api/employee",json).Result;
if (response.IsSuccessStatusCode)
{
TempData["msg"] = "" + employeeModel.EmailId + " updated successfully";
ModelState.Clear();
}
}
else
{
ModelState.AddModelError("EmailId", "Email Id already exists");
}
}
return View("_EditEmployeePartial", employeeModel);
}
Но когда я запускаю приложение, я получаю сообщение об ошибке с кодом состояния 400
{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content:
System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers:
{
Server: Kestrel
X-SourceFiles: =?UTF-8?B? RjpcQyBQYXJ0aXRpb25cRG9jdW1lbnRzXFRlamFzIFRyYWluaW5nXEVtcGxveWVlTWFuYWdlbWVudFN5c3RlbVxFbXBsb3llZU1hbmFnZW50LldlYlxXZWJBUElcYXBpXGVtcGxveWVl?=
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Date: Sat, 27 Oct 2018 07:54:27 GMT
Content-Length: 0
}}
Пожалуйста, помогите мне решить эту проблему .... !!!