我正试图使用以下语句调用api来更新webproject中的记录
public async Task UpdateEmployee(Employee employee)
{
var employeeJson =
new StringContent(JsonSerializer.Serialize(employee), Encoding.UTF8, "application/json");
await _httpClient.PutAsync("api/employee", employeeJson);
}
但当我调试变量employeeJson时,to显示错误"无法评估子项"数据被传递给可变员工,如下所示
{
"employeeId": 3,
"firstName": "sfdsfdsfsd",
"lastName": "fdgf",
"birthDate": "2020-09-30T19:09:26.075",
"email": "ppp@gmail.com",
"street": "sdfdsfdsf",
"zip": "sdfsdfds",
"city": "sdfsdf",
"countryId": 1,
"country": {
"countryId": 1,
"name": "Belgium"
},
"phoneNumber": "1211",
"maritalStatus": 0,
"gender": 0,
"comment": "sdfdsfds",
"joinedDate": "2020-09-30T19:09:26.124",
"exitDate": "2020-09-30T00:00:00",
"jobCategoryId": 1,
"jobCategory": {
"jobCategoryId": 1,
"jobCategoryName": "Pie research"
}
}
我的web api启动.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//services.AddDbContext<AppDbContext>(options => options.UseInMemoryDatabase(databaseName: "BethanysPieShopHRM"));
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddScoped<ICountryRepository, CountryRepository>();
services.AddScoped<IJobCategoryRepository, JobCategoryRepository>();
services.AddScoped<IEmployeeRepository, EmployeeRepository>();
services.AddCors(options =>
{
options.AddPolicy("Open", builder => builder.AllowAnyOrigin().AllowAnyHeader());
});
services.AddControllers();
//.AddJsonOptions(options => options.JsonSerializerOptions.ca);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseCors("Open");
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
回购以获取员工记录
public Employee GetEmployeeById(int employeeId)
{
return _appDbContext.Employees
.Include(e => e.Country)
.Include(e=>e.JobCategory)
.FirstOrDefault(c => c.EmployeeId == employeeId);
}
我试图通过邮递员更新,但错误消息显示为"错误405"使用资源不支持的请求方法请求资源
我从API控制器调用的方法如下所示
[HttpPut]
public IActionResult UpdateEmployee([FromBody] Employee employee)
{
if (employee == null)
return BadRequest();
if (employee.FirstName == string.Empty || employee.LastName == string.Empty)
{
ModelState.AddModelError("Name/FirstName", "The name or first name shouldn't be empty");
}
if (!ModelState.IsValid)
return BadRequest(ModelState);
var employeeToUpdate = _employeeRepository.GetEmployeeById(employee.EmployeeId);
if (employeeToUpdate == null)
return NotFound();
_employeeRepository.UpdateEmployee(employee);
return NoContent(); //success
}
您几乎可以使用依赖注入来获取值了。在.rarzor类中,如果您想使用存储库按ID获取员工,请执行以下操作。
[Inject]
public IEmployeeRepository EmployeeRepository {get;set;}
public void MyMethod()
{var employeeToUpdate=EmployeeRepository。GetEmployeeById(employee.EmployeeId(;
}